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
while (nodeIterator.MoveNext())
{
XPathNavigator n = nodeIterator.Current;
Console.WriteLine(n.LocalName);
}
while (nodeIterator->MoveNext())
{
XPathNavigator^ n = nodeIterator->Current;
Console::WriteLine(n->LocalName);
}
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
foreach (XPathNavigator n in nodeIterator)
Console.WriteLine(n.LocalName);
for each (XPathNavigator^ n in nodeIterator)
Console::WriteLine(n->LocalName);
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: