2012-09-20 72 views
1

我正在嘗試使用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

它的命名空間的問題。在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; 
+0

也看看下面的鏈接。 http://stackoverflow.com/questions/11691527/cant-get-inner-text-from-fql-using-xpath-c-sharp –