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

次の例では、クラスのメソッドをSelectXPathNavigator使用して、クラスを使用してノード セットを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 呼び出すと、ドキュメントの順序で次のノードに移動します。

クラスを使用XPathNodeIteratorしてコレクションを反復処理するにはXPathNavigator、2 つの方法があります。

1 つの方法は、次の例のように、メソッドをMoveNext使用し、現在のインスタンスをXPathNavigator取得するために呼び出Currentす方法です。

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

もう 1 つの方法は、次の例のように、ループを 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 必要があります。 これら 2 つの方法を組み合わせると、予期しない結果が発生する可能性があります。 たとえば、メソッドが最初に MoveNext 呼び出され、そのメソッド GetEnumerator がループ内で foreach 呼び出された場合、 foreach ループはコレクションの先頭からではなく、メソッドの後の位置から結果の列挙を Current 開始します。

適用対象

こちらもご覧ください