按索引检索已排序节点

万维网联合会 (W3C) XML 文档对象模型 (DOM) 还描述了 NodeList,后者能够处理已排序节点列表(与 XmlNamedNodeMap 处理的未排序集相对)。 NodeList 在 Microsoft .NET Framework 中称为 XmlNodeList。 返回 XmlNodeList 的方法和属性包括:

  • XmlNode.ChildNodes

  • XmlDocument.GetElementsByTagName

  • XmlElement.GetElementsByTagName

  • XmlNode.SelectNodes

XmlNodeList 包含的 Count 属性可用于编写循环,以循环访问 XmlNodeList 中的节点,如下面的代码示例所示:

 Dim doc as XmlDocument = new XmlDocument()
    doc.Load("books.xml")
                         
     ' Retrieve all book titles.
     Dim root as XmlElement = doc.DocumentElement
     Dim elemList as XmlNodeList = root.GetElementsByTagName("title")
     Dim i as integer
     for i=0  to elemList.Count-1
         ' Display all book titles in the Node List.
         Console.WriteLine(elemList.ItemOf(i).InnerXml)
     next
    
     XmlDocument doc = new XmlDocument();
     doc.Load("books.xml");
     // Retrieve all book titles.
     XmlElement root = doc.DocumentElement;
     XmlNodeList elemList = root.GetElementsByTagName("title");
     for (int i=0; i < elemList.Count; i++)
     {   
        // Display all book titles in the Node List.
        Console.WriteLine(elemList[i].InnerXml);
     } 

除了 Count 属性外,还有一个 GetEnumerator 方法提供对 XmlNodeList 中的节点集合的 foreach 式循环访问。 下面的代码示例显示如何使用 foreach 语句。

Dim doc As New XmlDocument()
doc.Load("books.xml")

' Get book titles.
Dim root As XmlElement = doc.DocumentElement
Dim elemList As XmlNodeList = root.GetElementsByTagName("title")
Dim ienum As IEnumerator = elemList.GetEnumerator()
' Loop over the XmlNodeList using the enumerator ienum        
While ienum.MoveNext()
    ' Display the book title.
    Dim title As XmlNode = CType(ienum.Current, XmlNode)
    Console.WriteLine(title.InnerText)
End While
{
     XmlDocument doc = new XmlDocument();
     doc.Load("books.xml");
                         
     // Get book titles.
     XmlElement root = doc.DocumentElement;
     XmlNodeList elemList = root.GetElementsByTagName("title");
     IEnumerator ienum = elemList.GetEnumerator();  
     // Loop over the XmlNodeList using the enumerator ienum        
     while (ienum.MoveNext())
     {
          // Display the book title.
           XmlNode title = (XmlNode) ienum.Current;
           Console.WriteLine(title.InnerText);
     }
  }

有关 XmlNodeList 的可用方法和属性的更多信息,请参见 XmlNodeList 成员

请参见

概念

XML 文档对象模型 (DOM)