XPathNavigator.Clone メソッド
アセンブリ: System.Xml (system.xml.dll 内)

Dim instance As XPathNavigator Dim returnValue As XPathNavigator returnValue = instance.Clone
この XPathNavigator と同じノードに配置された新しい XPathNavigator。

Clone メソッドは、XPathNodeIterator と組み合わせると特に役立ちます。XPathNodeIterator は、選択したノード セットを反復処理するために使用します。これには、XPathNodeIterator のコンテキスト ノードに配置された XPathNavigator を返す、Current プロパティが含まれています。ただし、Current プロパティで返された XPathNavigator を使用して、ノード セットから移動できません。代わりに、返された XPathNavigator の複製を作成し、複製したナビゲータを使用して追加の移動処理を行います。
クローンの XPathNavigator は、元の XPathNavigator をそれ以降変更しても影響を受けません。

Herman Melville の著書のタイトルをすべて取得する例を次に示します。
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
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); }
XPathDocument^ document = gcnew XPathDocument("books.xml"); XPathNavigator^ navigator = document->CreateNavigator(); XPathExpression^ query = navigator->Compile("/bookstore/book"); XPathNodeIterator^ nodes = navigator->Select(query); XPathNavigator^ nodesNavigator = nodes->Current; for each(XPathNavigator^ navigator in nodesNavigator-> SelectDescendants(XPathNodeType::Text, false)) { Console::Write(navigator->Name); Console::WriteLine(navigator->Value); }
この例では、入力として、contosoBooks.xml というファイルを使用しています。
<bookstore xmlns="http://www.contoso.com/books"> <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>

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- XPathNavigator.Clone メソッドのページへのリンク