XmlNode.GetEnumerator 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取循环访问当前节点中子节点的枚举数。
public:
 System::Collections::IEnumerator ^ GetEnumerator();public System.Collections.IEnumerator GetEnumerator ();member this.GetEnumerator : unit -> System.Collections.IEnumeratorPublic Function GetEnumerator () As IEnumerator返回
一个 IEnumerator 对象,可用于循环访问当前节点中的子节点。
示例
以下示例显示 XML 文档中的所有书籍。
#using <System.Xml.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew 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 = dynamic_cast<XmlNode^>(ienum->Current);
      Console::WriteLine( book->OuterXml );
      Console::WriteLine();
   }
}
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
此示例使用该文件 books.xml作为输入。
<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>
注解
为“每个”样式迭代提供对节点的支持 XmlNode。
此方法是文档对象模型 (DOM) 的 Microsoft 扩展。