从XML标签获取innertext?

时间:2012-09-20 07:54:43

标签: c# xml

我正在尝试使用XML标记的内部文本或值填充数组。我可以遍历标签的节点,我只是在拉出存储的值时遇到问题。

我正在循环问题底部的属性标记,XML的子节点。

    XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("C://Users//Shaun//Documents//Visual Studio 2010//Projects//LightStoneTestService//LightStoneTestService//FileTest//Testdo.xml");

//XmlNodeList dataNodes = xmlDoc.SelectNodes("//Properties");
XmlNodeList oXMlNodeList = xmlDoc.GetElementsByTagName("Properties");

for (int Count = 0; Count < 27; Count++)
{
    foreach (XmlNode node in oXMlNodeList)
    {                    
        int Max = node.ChildNodes.Count;                   
        foreach (XmlNode childNode in node.ChildNodes) //For each child node in FieldData
        {                          
              if (ArrayProperties[Count].LightStoneTag == childNode.Name)
            {
              ArrayProperties[Count].Value = node.SelectSingleNode(ArrayProperties[Count].LightStoneTag).InnerText;    
            }                                                     
        }
    }
}

** * ** 我的Xml文件如下所示:

<NewDataSet>
  <RequestData xmlns="RequestData">
    <Req_ID>3204eba5-07f4-4e83-8b46-d89fa1d70bf6</Req_ID>
  </RequestData>
  <Properties xmlns="Properties">
    <sr_id>19374324</sr_id>
    <prop_id>9841107</prop_id>
    <DEED_ID>21</DEED_ID>
    <PROPTYPE_ID>2</PROPTYPE_ID>
    <SS_ID>2315</SS_ID>
    <NAD_ID>3048001</NAD_ID>
    <property_type>SS</property_type>
    <PROVINCE>GA</PROVINCE>
    <MUNICNAME>CITY OF JOHANNESBURG</MUNICNAME>
    <DEEDTOWN>ALLENS NEK</DEEDTOWN>
    <SECTIONAL_TITLE>SS GREENHILLS</SECTIONAL_TITLE>
    <UNIT>15</UNIT>
    <TownShip>ALLENS NEK</TownShip>
    <Purchase_Price>236500</Purchase_Price>
    <Purchase_Date>20031020</Purchase_Date>
    <Bond_Number>SB37369/2006</Bond_Number>
    <Township_alt>ALLEN'S NEK EXT 32</Township_alt>
    <RE>false</RE>
  </Properties>
</NewDataSet>

1 个答案:

答案 0 :(得分:1)

它是命名空间问题。在for循环之前添加这部分代码:

        NameTable nt = new NameTable();
        nt.Add("Properties");
        XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
        nsmgr.AddNamespace("ns1", "Properties");

将检索InnerText的行更改为:

        ArrayProperties[Count].Value = node
            .SelectSingleNode(
                String.Concat("//ns1:", ArrayProperties[Count].LightStoneTag), 
                nsmgr)
            .InnerText;