XPathNavigator.Clone メソッド

定義

派生クラスでオーバーライドされると、この XPathNavigator と同じノードに配置される新しい XPathNavigator を作成します。When overridden in a derived class, creates a new XPathNavigator positioned at the same node as this XPathNavigator.

public:
 abstract System::Xml::XPath::XPathNavigator ^ Clone();
public abstract System.Xml.XPath.XPathNavigator Clone ();
abstract member Clone : unit -> System.Xml.XPath.XPathNavigator
Public MustOverride Function Clone () As XPathNavigator

戻り値

XPathNavigator

この XPathNavigator と同じノードに配置された新しい XPathNavigatorA new XPathNavigator positioned at the same node as this XPathNavigator.

次の例では、Herman Melville によって作成されたすべての書名を取得します。The following example gets all book titles authored by Herman Melville.

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

// Select all books authored by Melville.
XPathNodeIterator^ nodes = navigator->Select("descendant::book[author/last-name='Melville']");

while (nodes->MoveNext())
{
    // Clone the navigator returned by the Current property. 
    // Use the cloned navigator to get the title element.
    XPathNavigator^ clone = nodes->Current->Clone();
    clone->MoveToFirstChild();
    Console::WriteLine("Book title: {0}", clone->Value);
}
XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

// Select all books authored by Melville.
XPathNodeIterator nodes = navigator.Select("descendant::book[author/last-name='Melville']");

while (nodes.MoveNext())
{
    // Clone the navigator returned by the Current property.
    // Use the cloned navigator to get the title element.
    XPathNavigator clone = nodes.Current.Clone();
    clone.MoveToFirstChild();
    Console.WriteLine("Book title: {0}", clone.Value);
}
Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

' Select all books authored by Melville.
Dim nodes As XPathNodeIterator = navigator.Select("descendant::book[author/last-name='Melville']")

While nodes.MoveNext()
    ' Clone the navigator returned by the Current property. 
    ' Use the cloned navigator to get the title element.
    Dim clone As XPathNavigator = nodes.Current.Clone()
    clone.MoveToFirstChild()
    Console.WriteLine("Book title: {0}", clone.Value)
End While

この例は、books.xml ファイルを入力として使用します。The example takes the books.xml file as an input.

<?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>  

注釈

Cloneメソッドは、と組み合わせて使用すると特に便利です XPathNodeIteratorThe Clone method is especially useful in conjunction with the XPathNodeIterator. XPathNodeIteratorは、選択したノードセットを反復処理するために使用され Current ます。には XPathNavigator 、のコンテキストノード上に位置するを返すプロパティが含まれてい XPathNodeIterator ます。An XPathNodeIterator is used to iterate over a selected node set, and contains a Current property which returns an XPathNavigator positioned on the context node of the XPathNodeIterator. ただし、 XPathNavigator プロパティによって返されるは、 Current ノードセットから離れた場所に移動するために使用することはできません。However, the XPathNavigator returned by the Current property cannot be used to move away from the node set. 代わりに、返されたを複製し、複製した XPathNavigator ナビゲーターを使用して追加の移動を行います。Instead, you clone the returned XPathNavigator and use the cloned navigator to do any additional moves.

複製されたは、 XPathNavigator 元のに対する以降の変更の影響を受けません XPathNavigatorThe cloned XPathNavigator is not affected by subsequent changes to the original XPathNavigator.

適用対象