【问题标题】:Iterating through all nodes in XML file遍历 XML 文件中的所有节点
【发布时间】:2010-05-26 17:25:22
【问题描述】:

我想遍历 XML 文件中的所有节点并打印它们的名称。 做这个的最好方式是什么?我正在使用 .NET 2.0。

【问题讨论】:

    标签: c# xml


    【解决方案1】:

    您可以使用XmlDocument。一些 XPath 也很有用。

    只是一个简单的例子

    XmlDocument doc = new XmlDocument();
    doc.Load("sample.xml");
    XmlElement root = doc.DocumentElement;
    XmlNodeList nodes = root.SelectNodes("some_node"); // You can also use XPath here
    foreach (XmlNode node in nodes)
    {
       // use node variable here for your beeds
    }
    

    【讨论】:

    • 更喜欢这个,因为与使用 XmlReader 相比,它将开始/结束元素和内容视为单个项目
    • 不确定为什么 foreach(节点中的 XmlNode 节点)只循环通过 1 个顶部节点,例如(“some_node”),而没有到达它下面的嵌套子节点等等......在(XmlNodeList节点)。你能帮忙吗?
    【解决方案2】:

    我认为最快和最简单的方法是使用XmlReader,这不需要任何递归和最小的内存占用。

    这是一个简单的例子,为了简洁起见,我只使用了一个简单的字符串,当然你可以使用来自文件等的流。

      string xml = @"
        <parent>
          <child>
            <nested />
          </child>
          <child>
            <other>
            </other>
          </child>
        </parent>
        ";
    
      XmlReader rdr = XmlReader.Create(new System.IO.StringReader(xml));
      while (rdr.Read())
      {
        if (rdr.NodeType == XmlNodeType.Element)
        {
          Console.WriteLine(rdr.LocalName);
        }
      }
    

    上面的结果将是

    parent
    child
    nested
    child
    other
    

    XML 文档中所有元素的列表。

    【讨论】:

      【解决方案3】:

      这是我很快为自己写的:

      public static class XmlDocumentExtensions
      {
          public static void IterateThroughAllNodes(
              this XmlDocument doc, 
              Action<XmlNode> elementVisitor)
          {
              if (doc != null && elementVisitor != null)
              {
                  foreach (XmlNode node in doc.ChildNodes)
                  {
                      doIterateNode(node, elementVisitor);
                  }
              }
          }
      
          private static void doIterateNode(
              XmlNode node, 
              Action<XmlNode> elementVisitor)
          {
              elementVisitor(node);
      
              foreach (XmlNode childNode in node.ChildNodes)
              {
                  doIterateNode(childNode, elementVisitor);
              }
          }
      }
      

      为了使用它,我使用了类似的东西:

      var doc = new XmlDocument();
      doc.Load(somePath);
      
      doc.IterateThroughAllNodes(
          delegate(XmlNode node)
          {
              // ...Do something with the node...
          });
      

      也许它可以帮助那里的人。

      【讨论】:

      • 这太棒了!通过单步执行这段代码,我学到了很多东西,谢谢分享。
      • 通用方法。 +1 我更喜欢这个。
      【解决方案4】:

      遍历所有元素

      XDocument xdoc = XDocument.Load("input.xml");
      foreach (XElement element in xdoc.Descendants())
      {
          Console.WriteLine(element.Name);
      }
      

      【讨论】:

      【解决方案5】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-01
        • 1970-01-01
        • 2014-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多