次の方法で共有


XPathNavigator.Clone メソッド

この XPathNavigator と同じノードに配置される新しい XPathNavigator を作成します。

Public MustOverride Function Clone() As XPathNavigator
[C#]
public abstract XPathNavigator Clone();
[C++]
public: virtual XPathNavigator* Clone() = 0;
[JScript]
public abstract function Clone() : XPathNavigator;

戻り値

元の XPathNavigator と同じノードに配置される新しい XPathNavigator オブジェクト。

解説

このメソッドは、 XPathNodeIterator と共に使用すると特に有効です。 XPathNodeIterator は、選択されたノード セットを反復処理するために使用され、 XPathNodeIterator のコンテキスト ノードに配置された XPathNavigator を返す Current プロパティを格納します。ただし、Current プロパティで返された XPathNavigator を使用して、ノード セットから移動できません。代わりに、返された XPathNavigator のクローンを作成し、クローンのナビゲータを使用して追加移動する必要があります。

クローンの XPathNavigator は、元の XPathNavigator をそれ以降変更しても影響を受けません。

使用例

[Visual Basic, C#, C++] Jane Austen の著書のタイトルをすべて取得する例を次に示します。

 
Imports System
Imports System.Xml.XPath

public class Sample

  public shared sub Main()

    Dim doc as XPathDocument = new XPathDocument("booksort.xml")
    Dim nav as XPathNavigator = doc.CreateNavigator()

    ' Select all books authored by Austen.
    Dim ni as XPathNodeIterator = nav.Select("descendant::book[author/last-name='Austen']")
    while (ni.MoveNext())
       ' Clone the navigator returned by the Current property. 
       ' Use the cloned the navigator to get the title element.
       Dim clone as XPathNavigator = ni.Current.Clone()
       clone.MoveToFirstChild()
       Console.WriteLine("Book title: {0}", clone.Value)
    end while
  end sub
end class

[C#] 
using System;
using System.Xml.XPath;

public class Sample
{
  public static void Main()
  {

    XPathDocument doc = new XPathDocument("booksort.xml");
    XPathNavigator nav = doc.CreateNavigator();

    // Select all books authored by Austen.
    XPathNodeIterator ni = nav.Select("descendant::book[author/last-name='Austen']");
    while (ni.MoveNext()){
       // Clone the navigator returned by the Current property. 
       // Use the cloned the navigator to get the title element.
       XPathNavigator clone = ni.Current.Clone();
       clone.MoveToFirstChild();
       Console.WriteLine("Book title: {0}", clone.Value);
    }
  }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml::XPath;

int main()
{
   XPathDocument* doc = new XPathDocument(S"booksort.xml");
   XPathNavigator * nav = doc -> CreateNavigator();

   // Select all books authored by Austen.
   XPathNodeIterator * ni = nav -> Select(S"descendant::book->Item[author/last-name='Austen']");
   while (ni -> MoveNext())
   {
      // Clone the navigator returned by the Current property. 
      // Use the cloned the navigator to get the title element.
      XPathNavigator * clone = ni -> Current -> Clone();
      clone -> MoveToFirstChild();
      Console::WriteLine(S"Book title: {0}", clone -> Value);
   }
}

[Visual Basic, C#, C++] この例では、入力として、 booksort.xml というファイルを使用しています。

<?xml version="1.0"?>
<!-- a fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
  <book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
    <title>Pride And Prejudice</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>24.95</price>
  </book>
  <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
    <title>The Handmaid's Tale</title>
    <author>
      <first-name>Margaret</first-name>
      <last-name>Atwood</last-name>
    </author>
    <price>29.95</price>
  </book>
  <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
    <title>Emma</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
  <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
    <title>Sense and Sensibility</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
</bookstore>

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

XPathNavigator クラス | XPathNavigator メンバ | System.Xml.XPath 名前空間