Export (0) Print
Expand All

Ordered Node Retrieval by Index

The World Wide Web Consortium (W3C) XML Document Object Model (DOM) also describes a NodeList, which has the ability to handle an ordered list of nodes, as opposed to the unordered set handled by the XmlNamedNodeMap. The NodeList in the Microsoft .NET Framework is called XmlNodeList. Methods and properties that return an XmlNodeList are:

  • XmlNode.ChildNodes

  • XmlDocument.GetElementsByTagName

  • XmlElement.GetElementsByTagName

  • XmlNode.SelectNodes

The XmlNodeList has a Count property that can be used to write loops to iterate over the nodes in the XmlNodeList, as shown in the following code sample:

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

In addition to the Count property, there is a GetEnumerator method that provides a, foreach style iteration over the collection of nodes in the XmlNodeList. The following code example shows the use of the foreach statement.

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

For more information on the methods and properties available on the XmlNodeList, see XmlNodeList Members.

Show:
© 2014 Microsoft