XPathNodeIterator Class
This page is specific to:.NET Framework Version:1.12.03.03.54.0
.NET Framework Class Library
XPathNodeIterator Class

Provides an iterator over a selected set of nodes.

Namespace:  System.Xml.XPath
Assembly:  System.Xml (in System.Xml.dll)
Syntax

'Usage

Dim instance As XPathNodeIterator

'Declaration

Public MustInherit Class XPathNodeIterator _
    Implements ICloneable, IEnumerable
Remarks

An XPathNodeIterator object returned by the XPathNavigator class is not positioned on the first node in a selected set of nodes. A call to the MoveNext method of the XPathNodeIterator class must be made to position the XPathNodeIterator object on the first node in the selected set of nodes.

When using the XPathNodeIterator, if you edit the current node or any of its ancestors, your current position is lost. If you want to edit a number of nodes that you have selected, create a XPathNavigator array, copy all of the nodes from the XPathNodeIterator into the array, then iterate through the array and modify the nodes.

There are two ways to iterate over an XPathNavigator collection by using the XPathNodeIterator class.

One way is to use MoveNext and then call Current to get the current XPathNavigator instance, as in the following example:

While nodeIterator.MoveNext()
    Dim n As XPathNavigator = nodeIterator.Current
    Console.WriteLine(n.LocalName)
End While


Another way is to use a foreach loop to call GetEnumerator and use the returned IEnumerator interface to enumerate the nodes, as in the following example:

For Each n As XPathNavigator In nodeIterator
    Console.WriteLine(nav.LocalName)
Next


You should either use MoveNext and Current or use GetEnumerator. Combining these two approaches can cause unexpected results. For example, if the MoveNext is called first, and then GetEnumerator is called in the foreach loop, the foreach loop will not start enumerating the results from the beginning of the collection, but from the position after the Current method.

Notes to Inheritors:

When you inherit from the XPathNodeIterator class, you must override the following members:

Examples

The following example uses the Select method of the XPathNavigator class to select a node set using the XPathNodeIterator class.

Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

Dim nodes As XPathNodeIterator = navigator.Select("/bookstore/book")
nodes.MoveNext()
Dim nodesNavigator As XPathNavigator = nodes.Current

Dim nodesText As XPathNodeIterator = nodesNavigator.SelectDescendants(XPathNodeType.Text, False)

While nodesText.MoveNext()
    Console.WriteLine(nodesText.Current.Value)
End While


The example takes the books.xml file as input.

<bookstore>
    <book genre="autobiography" publicationdate="1981-03-22" 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-11-17" 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-02-15" ISBN="1-861001-57-6">
        <title>The Gorgias</title>
        <author>
            <name>Plato</name>
        </author>
        <price>9.99</price>
    </book>
</bookstore>
Inheritance Hierarchy

System..::.Object
  System.Xml.XPath..::.XPathNodeIterator
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Community Content

You can use foreach
Added by:cheeso
  

XmlDocument doc = new XmlDocument();
doc.Load("orders.xml");
XPathNavigator nav = doc.CreateNavigator();
foreach (XPathNavigator node in nav.Select("/orders/order"))
Console.WriteLine(node.Value);

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View