2012-08-01 70 views
14

我有一個XML文件,我想遍歷每個子節點收集信息。如何迭代XML文件中的每個子節點?

這是我的C#代碼,它只拾取一個節點,FieldData我想在其子節點上使用foreach。

public void LoadXML() { 
    if (File.Exists("Data.xml")) { 
     //Reading XML 
     XmlDocument xmlDoc = new XmlDocument(); 
     xmlDoc.Load("Data.xml"); 

     //Think something needs to reference Child nodes, so i may Foreach though them 

     XmlNodeList dataNodes = xmlDoc.SelectNodes("//FieldData"); 
     TagContents[] ArrayNode; 

     foreach(XmlNode node in dataNodes) { 
      int Count = 0; 
      //int Max = node.ChildNodes.Count; 
      ArrayNode = new TagContents[Max]; 

      ArrayNode[Count].TagName = node.Name; 
      ArrayNode[Count].TagValue = node.SelectSingleNode(ArrayNode[Count].TagName).InnerText; 
      Count = Count + 1;   
     } 
    } else { 
     MessageBox.Show("Could not find file Data.xml"); 
    } 
} 

我的XML看起來像:

<?xml version="1.0"?> 
<FieldData> 
    <property_details_branch IncludeInPDFExport="Yes" Mod="20010101010101"/> 
    <property_details_inspection_date IncludeInPDFExport="Yes" Mod="20120726200230">20120727220230+0200</property_details_inspection_date> 
    <property_details_type_of_ownership IncludeInPDFExport="Yes" Mod="20120726134107">Freehold</property_details_type_of_ownership> 
</FieldData> 
+0

我只是想指出,在'foreach'循環設置'Count'爲零,做的東西,增量它並且設置回零。 – 2017-09-16 08:25:09

回答

18

你迭代FieldData節點,您只有一個。遍歷其子節點寫:

foreach (XmlNode node in dataNodes) 
{ 
    foreach (XmlNode childNode in node.ChildNodes) 
    { 
+1

不應該是foreach(node.ChildNodes中的XmlNode childNode)? – Rox 2012-08-01 12:53:17

+0

你說得對。我習慣了XDocument – 2012-08-01 12:54:29

+0

感謝它的作品:我奮鬥了這麼久。 – Pomster 2012-08-01 12:57:15

5

我一般喜歡Linq-To-Xml對於這種事情:

var doc = XDocument.Load("XMLFile1.xml"); 
    foreach (var child in doc.Element("FieldData").Elements()) 
    { 
    Console.WriteLine(child.Name); 
    } 
2

你可以這樣說:

XDocument doc = XDocument.Load(@"Data.xml"); 
    TagContents[] ArrayNode = doc.Root 
           .Elements() 
           .Select(el => 
            new TagContents() 
            { 
             TagName = el.Name.ToString(), 
             TagValue = el.Value 
            }) 
           .ToArray(); 
4

或者您使用遞歸:

public void findAllNodes(XmlNode node) 
    { 
     Console.WriteLine(node.Name); 
     foreach (XmlNode n in node.ChildNodes) 
      findAllNodes(n); 
    } 

您在哪裏放置有效負載取決於您想要使用哪種搜索(例如,廣度優先搜索,深度優先搜索等;請參閱http://en.wikipedia.org/wiki/Euler_tour_technique

0

只需觸及@Waynes的答案,即可正常工作。我用下面的代碼,以進一步推入子節點在我的XML

foreach (var child in doc.Element("rootnodename").Element("nextchildnode").Elements()) 

{ 

//do work here, probs async download xml content to file on local disc 

} 
0
public void ValidateXml(string[] Arrays) 
    {           
     foreach (var item in Arrays) 
     { 
      Xdoc.Load(item);        
      XmlNodeList xnList = Xdoc.SelectNodes("FirstParentNode"); 
      if (xnList.Count > 0) 
      { 
       foreach (XmlNode xn in xnList) 
       { 
        XmlNodeList anode = xn.SelectNodes("SecondParentNode"); 
        if (anode.Count > 0) 
        { 
         foreach (XmlNode bnode in anode) 
         {        
          string InnerNodeOne = bnode["InnerNode1"].InnerText; 
          string InnerNodeTwo = bnode["InnerNode1"].InnerText; 

         }       
        } 
        else 
        { 
         ErrorLog("Parent Node DoesNot Exists");             
        } 
       }     
      } 
      else 
      { 
       ErrorLog("Parent Node DoesNot Exists"); 
      } 

     } 
     //then insert or update these values in database here 
    }