共用方式為


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 的位置開始列舉結果。

適用於

另請參閱