Прочетете на английски Редактиране

Споделяне чрез


XmlNodeList.GetEnumerator Method

Definition

Gets an enumerator that iterates through the collection of nodes.

C#
public abstract System.Collections.IEnumerator GetEnumerator();

Returns

An enumerator used to iterate through the collection of nodes.

Implements

Examples

The following example displays all the book titles.

C#
using System;
using System.IO;
using System.Xml;
using System.Collections;

public class Sample {

  public static void Main() {

     XmlDocument doc = new XmlDocument();
     doc.Load("2books.xml");

     //Get and display all the book titles.
     XmlElement root = doc.DocumentElement;
     XmlNodeList elemList = root.GetElementsByTagName("title");
     IEnumerator ienum = elemList.GetEnumerator();
     while (ienum.MoveNext()) {
       XmlNode title = (XmlNode) ienum.Current;
       Console.WriteLine(title.InnerText);
     }
  }
}

The example uses the file 2books.xml as input.

XML
<!--sample XML fragment-->
<bookstore>
  <book genre='novel' ISBN='10-861003-324'>
    <title>The Handmaid's Tale</title>
    <price>19.95</price>
  </book>
  <book genre='novel' ISBN='1-861001-57-5'>
    <title>Pride And Prejudice</title>
    <price>24.95</price>
  </book>
</bookstore>

Remarks

Provides a simple "foreach" style iteration over the collection of nodes in the XmlNodeList.

Applies to

Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0