通过


XPathNodeIterator.MoveNext 方法

定义

在派生类中重写时,将XPathNavigatorCurrent属性返回的对象移到所选节点集中的下一个节点。

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

返回

如果对象 移动到下一个节点, 则为如果没有更多选定的节点。

示例

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

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>

注解

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

使用类循环访问 XPathNavigator 集合 XPathNodeIterator 有两种方法。

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

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

foreach (XPathNavigator n in nodeIterator)
    Console.WriteLine(n.LocalName);
For Each n As XPathNavigator In nodeIterator
    Console.WriteLine(nav.LocalName)
Next

应使用MoveNext该方法或使用CurrentGetEnumerator该方法。 结合这两种方法可能会导致意外结果。 例如,如果 MoveNext 首先调用该方法,然后在 GetEnumerator 循环中 foreach 调用该方法,则 foreach 循环不会从集合的开头开始枚举结果,而是从方法之后 Current 的位置开始枚举结果。

适用于

另请参阅