如何迭代XML文件中的每个子节点?

如何迭代XML文件中的每个子节点?

问题描述:

我有一个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

你迭代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

我一般喜欢Linq-To-Xml对于这种事情:

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

你可以这样说:

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

或者您使用递归:

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

只需触及@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 

} 

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 
    }