遍历XML文件中的所有节点

时间:2022-12-31 14:12:20

I want to iterate through all nodes in an XML file and print their names. What is the best way to do this? I am using .NET 2.0.

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

5 个解决方案

#1


30  

I think the fastest and simplest way would be to use an XmlReader, this will not require any recursion and minimal memory foot print.

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

Here is a simple example, for compactness I just used a simple string of course you can use a stream from a file etc.

这里有一个简单的例子,对于紧凑性我只使用了一个简单的字符串当然你可以使用一个文件中的流等等。

  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);
    }
  }

The result of the above will be

以上的结果将会是。

parent
child
nested
child
other

A list of all the elements in the XML document.

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

#2


35  

You can use XmlDocument. Also some XPath can be useful.

您可以使用XmlDocument。还可以使用一些XPath。

Just a simple example

只是一个简单的例子

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
}

#3


14  

This is what I quickly wrote for myself:

这是我很快为自己写的:

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);
        }
    }
}

To use it, I've used something like:

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

var doc = new XmlDocument();
doc.Load(somePath);

doc.IterateThroughAllNodes(
    delegate(XmlNode node)
    {
        // ...Do something with the node...
    });

Maybe it helps someone out there.

也许它能帮助别人。

#4


3  

To iterate through all elements

遍历所有元素

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

#5


2  

A recursive algorithm that parses through an XmlDocument

通过XmlDocument解析的递归算法

Here is an example - Recursively reading an xml document and using regex to get contents

下面是一个例子——递归地读取xml文档并使用regex获取内容

Here is another recursive example - http://www.java2s.com/Tutorial/CSharp/0540__XML/LoopThroughXmlDocumentRecursively.html

下面是另一个递归示例——http://www.java2s.com/Tutorial/CSharp/0540__XML/LoopThroughXmlDocumentRecursively.html

#1


30  

I think the fastest and simplest way would be to use an XmlReader, this will not require any recursion and minimal memory foot print.

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

Here is a simple example, for compactness I just used a simple string of course you can use a stream from a file etc.

这里有一个简单的例子,对于紧凑性我只使用了一个简单的字符串当然你可以使用一个文件中的流等等。

  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);
    }
  }

The result of the above will be

以上的结果将会是。

parent
child
nested
child
other

A list of all the elements in the XML document.

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

#2


35  

You can use XmlDocument. Also some XPath can be useful.

您可以使用XmlDocument。还可以使用一些XPath。

Just a simple example

只是一个简单的例子

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
}

#3


14  

This is what I quickly wrote for myself:

这是我很快为自己写的:

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);
        }
    }
}

To use it, I've used something like:

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

var doc = new XmlDocument();
doc.Load(somePath);

doc.IterateThroughAllNodes(
    delegate(XmlNode node)
    {
        // ...Do something with the node...
    });

Maybe it helps someone out there.

也许它能帮助别人。

#4


3  

To iterate through all elements

遍历所有元素

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

#5


2  

A recursive algorithm that parses through an XmlDocument

通过XmlDocument解析的递归算法

Here is an example - Recursively reading an xml document and using regex to get contents

下面是一个例子——递归地读取xml文档并使用regex获取内容

Here is another recursive example - http://www.java2s.com/Tutorial/CSharp/0540__XML/LoopThroughXmlDocumentRecursively.html

下面是另一个递归示例——http://www.java2s.com/Tutorial/CSharp/0540__XML/LoopThroughXmlDocumentRecursively.html