Прочитај на енглеском Уреди

Делите путем


XPathNodeIterator.GetEnumerator Method

Definition

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

C#
public virtual System.Collections.IEnumerator GetEnumerator();

Returns

An IEnumerator object to iterate through the selected node set.

Implements

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 the MoveNext method and then call Current to get the current XPathNavigator instance, as in the following example:

C#
while (nodeIterator.MoveNext())
{
    XPathNavigator n = nodeIterator.Current;
    Console.WriteLine(n.LocalName);
}

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:

C#
foreach (XPathNavigator n in nodeIterator)
    Console.WriteLine(n.LocalName);

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

Производ Верзије
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also