XPathNodeIterator.MoveNext Method


When overridden in a derived class, moves the XPathNavigator object returned by the Current property to the next node in the selected node set.

 abstract bool MoveNext();
public abstract bool MoveNext ();
abstract member MoveNext : unit -> bool
Public MustOverride Function MoveNext () As Boolean



true if the XPathNavigator object moved to the next node; false if there are no more selected nodes.


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

XPathDocument^ document = gcnew XPathDocument("books.xml");
XPathNavigator^ navigator = document->CreateNavigator();

XPathNodeIterator^ nodes = navigator->Select("/bookstore/book");
XPathNavigator^ nodesNavigator = nodes->Current;

XPathNodeIterator^ nodesText = nodesNavigator->SelectDescendants(XPathNodeType::Text, false);

while (nodesText->MoveNext())
XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

XPathNodeIterator nodes = navigator.Select("/bookstore/book");
XPathNavigator nodesNavigator = nodes.Current;

XPathNodeIterator nodesText = nodesNavigator.SelectDescendants(XPathNodeType.Text, false);

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

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

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

While nodesText.MoveNext()
End While

The example takes the books.xml file as input.

<?xml version="1.0" encoding="utf-8" ?>   
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">  
        <title>The Autobiography of Benjamin Franklin</title>  
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">  
        <title>The Confidence Man</title>  
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">  
        <title>The Gorgias</title>  


The XPathNodeIterator object is positioned on the first node in the selected node set only after the initial call to the MoveNext method. The node set is created in document order. Therefore, calling the MoveNext method moves to the next node in document order.

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

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

while (nodeIterator->MoveNext())
    XPathNavigator^ n = nodeIterator->Current;
while (nodeIterator.MoveNext())
    XPathNavigator n = nodeIterator.Current;
While nodeIterator.MoveNext()
    Dim n As XPathNavigator = nodeIterator.Current
End While

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

for each (XPathNavigator^ n in nodeIterator)
foreach (XPathNavigator n in nodeIterator)
For Each n As XPathNavigator In nodeIterator

You should either use the MoveNext method and Current or use the GetEnumerator method. Combining these two approaches can cause unexpected results. For example, if the MoveNext method is called first, and then the GetEnumerator method 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.

Applies to

See also