XPathNodeIterator.GetEnumerator 方法

定义

返回 IEnumerator 对象以循环访问所选的节点集。Returns an IEnumerator object to iterate through the selected node set.

public:
 virtual System::Collections::IEnumerator ^ GetEnumerator();
public virtual System.Collections.IEnumerator GetEnumerator ();
abstract member GetEnumerator : unit -> System.Collections.IEnumerator
override this.GetEnumerator : unit -> System.Collections.IEnumerator
Public Overridable Function GetEnumerator () As IEnumerator

返回

循环访问所选节点集的 IEnumerator 对象。An IEnumerator object to iterate through the selected node set.

实现

注解

枚举器定位在 XPathNodeIterator 对象的当前位置。The enumerator is positioned on the current position of the XPathNodeIterator object.

可以通过两种方法使用 XPathNodeIterator 类来循环访问 XPathNavigator 集合。There are two ways to iterate over an XPathNavigator collection by using the XPathNodeIterator class.

一种方法是使用 MoveNext 方法,然后调用 Current 以获取当前 XPathNavigator 实例,如以下示例中所示: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;
Console::WriteLine(n->LocalName);
}
while (nodeIterator.MoveNext())
{
    XPathNavigator n = nodeIterator.Current;
    Console.WriteLine(n.LocalName);
}
While nodeIterator.MoveNext()
    Dim n As XPathNavigator = nodeIterator.Current
    Console.WriteLine(n.LocalName)
End While

另一种方法是使用 foreach 循环来调用 GetEnumerator 方法,并使用返回的 IEnumerator 接口枚举节点,如以下示例中所示: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)
Console::WriteLine(n->LocalName);
foreach (XPathNavigator n in nodeIterator)
    Console.WriteLine(n.LocalName);
For Each n As XPathNavigator In nodeIterator
    Console.WriteLine(nav.LocalName)
Next

应使用 MoveNext 方法,并 Current 或使用 GetEnumerator 方法。You should either use the MoveNext method and Current or use the GetEnumerator method. 组合这两种方法可能会导致意外的结果。Combining these two approaches can cause unexpected results. 例如,如果首先调用 MoveNext 方法,然后在 foreach 循环中调用 GetEnumerator 方法,则 foreach 循环不会开始从集合的开头枚举结果,而是从 Current 方法之后的位置进行枚举。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.

适用于

另请参阅