.NET Framework Class Library
XPathNodeIterator..::.GetEnumerator Method

Returns an IEnumerator object to iterate through the selected node set.

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

Visual Basic (Declaration)
Public Overridable Function GetEnumerator As IEnumerator
Visual Basic (Usage)
Dim instance As XPathNodeIterator
Dim returnValue As IEnumerator

returnValue = instance.GetEnumerator()
C#
public virtual IEnumerator GetEnumerator()
Visual C++
public:
virtual IEnumerator^ GetEnumerator()
JScript
public function GetEnumerator() : IEnumerator

Return Value

Type: System.Collections..::.IEnumerator
An IEnumerator object to iterate through the selected node set.

Implements

IEnumerable..::.GetEnumerator()()()
Remarks

The enumerator is positioned on the current position of the XPathNodeIterator object.

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:

Visual Basic
While nodeIterator.MoveNext()
    Dim n As XPathNavigator = nodeIterator.Current
    Console.WriteLine(n.LocalName)
End While
C#
while (nodeIterator.MoveNext())
{
    XPathNavigator n = nodeIterator.Current;
    Console.WriteLine(n.LocalName);
}
Visual C++
        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:

Visual Basic
For Each n As XPathNavigator In nodeIterator
    Console.WriteLine(nav.LocalName)
Next
C#
foreach (XPathNavigator n in nodeIterator)
    Console.WriteLine(n.LocalName);
Visual C++
        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.

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
See Also

Reference

Tags :


Page view tracker