19

我有一个 XML 文件,我想遍历收集信息的每个子节点。

这是我的 C# 代码,它只选择一个节点,即我想在其子节点上使用 foreach 的 FieldData。

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>
4

7 回答 7

26

您正在迭代 FieldData 节点,而您只有一个。要迭代其子节点,请编写:

foreach (XmlNode node in dataNodes)
{
     foreach (XmlNode childNode in node.ChildNodes)
     {
于 2012-08-01T12:45:59.123 回答
7

对于这种事情,我通常更喜欢Linq-To-Xml :

  var doc = XDocument.Load("XMLFile1.xml");
  foreach (var child in doc.Element("FieldData").Elements())
  {
    Console.WriteLine(child.Name);
  }
于 2012-08-01T12:53:52.510 回答
5

或者你使用递归:

    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

于 2012-08-02T06:20:56.950 回答
2

你可以这样做:

    XDocument doc = XDocument.Load(@"Data.xml");
    TagContents[] ArrayNode = doc.Root
                                .Elements()
                                .Select(el =>
                                    new TagContents()
                                    {
                                        TagName = el.Name.ToString(),
                                        TagValue = el.Value
                                    })
                                .ToArray();
于 2012-08-01T12:54:55.417 回答
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

} 
于 2015-06-20T23:32:32.550 回答
0

要遍历每个子节点、子节点等,我们必须使用Recursion. 万一有人和我有同样的要求,我实现了如下 -

public string ReadAllNodes(XmlNode node)
{
    if (node.ChildNodes.Count > 0)
    {
        foreach (XmlNode subNode in node)
        {
            //Recursion
            ReadAllNodes(subNode);
        }
    }
    else //Get the node value.
    {
        finalText = finalText + node.InnerText + System.Environment.NewLine;
    }
    return finalText;
}
于 2018-09-13T13:08:13.077 回答
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
    }
于 2017-07-18T07:15:33.110 回答