Language

XmlNode.GetEnumerator Method

Definition

Gets an enumerator that iterates through the child nodes in the current node.

public:
 System::Collections::IEnumerator ^ GetEnumerator();
public System.Collections.IEnumerator GetEnumerator();
member this.GetEnumerator : unit -> System.Collections.IEnumerator
Public Function GetEnumerator () As IEnumerator

Returns

An IEnumerator object that can be used to iterate through the child nodes in the current node.

Examples

The following example displays all the books in the XML document.

using System;
using System.Collections;
using System.Xml;

public class Sample {

  public static void Main() {

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

    Console.WriteLine("Display all the books...");
    XmlNode root = doc.DocumentElement;
    IEnumerator ienum = root.GetEnumerator();
    XmlNode book;
    while (ienum.MoveNext())
    {
      book = (XmlNode) ienum.Current;
      Console.WriteLine(book.OuterXml);
      Console.WriteLine();
    }
  }
}
Imports System.Collections
Imports System.Xml

public class Sample

  public shared sub Main()
  
    Dim doc as XmlDocument = new XmlDocument()
    doc.Load("books.xml")

    Console.WriteLine("Display all the books...")
    Dim root as XmlNode = doc.DocumentElement
    Dim ienum as IEnumerator = root.GetEnumerator()
    Dim book as XmlNode
    while (ienum.MoveNext())      
      book = CType(ienum.Current, XmlNode)
      Console.WriteLine(book.OuterXml)
      Console.WriteLine()
    end while

  end sub
end class

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





  
    The Autobiography of Benjamin Franklin
    
      Benjamin
      Franklin
    
    8.99
  
  
    The Confidence Man
    
      Herman
      Melville
    
    11.99
  
  
    The Gorgias
    
      Plato
    
    9.99
  

Remarks

Provides support for the "for each" style iteration over the nodes in the XmlNode.

This method is a Microsoft extension to the Document Object Model (DOM).

Applies to