Ordered Node Retrieval by Index
The W3C 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 is in the .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:
Dim doc as XmlDocument = new XmlDocument()
doc.Load("books.xml")
' Retrive 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
[C#]
XmlDocument doc = new XmlDocument();
doc.Load("books.xml");
// Retrive 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.
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
[C#]
{
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.