XPathNodeIterator.MoveNext 方法

定义

在派生类中重写时,将 Current 属性返回的 XPathNavigator 对象移至选定节点集中的下一个节点。

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

返回

Boolean

如果 XPathNavigator 对象已移至下一个节点,则为 true;如果没有其他选定节点,则为 false

示例

以下示例使用 Select 类的方法 XPathNavigator 选择使用该类的 XPathNodeIterator 节点集。

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

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

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

while (nodesText->MoveNext())
    Console::WriteLine(nodesText->Current->Value);
XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

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

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

while (nodesText.MoveNext())
    Console.WriteLine(nodesText.Current.Value);
Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

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

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

While nodesText.MoveNext()
    Console.WriteLine(nodesText.Current.Value)
End While

该示例使用 books.xml 文件作为输入。

<?xml version="1.0" encoding="utf-8" ?>   
<bookstore>  
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">  
        <title>The Autobiography of Benjamin Franklin</title>  
        <author>  
            <first-name>Benjamin</first-name>  
            <last-name>Franklin</last-name>  
        </author>  
        <price>8.99</price>  
    </book>  
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">  
        <title>The Confidence Man</title>  
        <author>  
            <first-name>Herman</first-name>  
            <last-name>Melville</last-name>  
        </author>  
        <price>11.99</price>  
    </book>  
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">  
        <title>The Gorgias</title>  
        <author>  
            <name>Plato</name>  
        </author>  
        <price>9.99</price>  
    </book>  
</bookstore>  

注解

对象 XPathNodeIterator 位于所选节点集中的第一个节点上,仅在对该方法进行初始调用 MoveNext 之后。 节点集按文档顺序创建。 因此,调用该方法将 MoveNext 按文档顺序移动到下一个节点。

可通过两种方法循环访问 XPathNavigator 集合,方法是使用 XPathNodeIterator 类。

一种方法是使用 MoveNext 该方法,然后调用 Current 以获取当前 XPathNavigator 实例,如以下示例所示:

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 的接口枚举节点,如以下示例所示:

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 该方法。 合并这两种方法可能会导致意外结果。 例如,如果 MoveNext 首先调用该方法,然后在 GetEnumerator 循环中 foreach 调用该方法,则 foreach 循环不会从集合的开头开始枚举结果,而是从方法之后 Current 的位置开始枚举结果。

适用于

另请参阅