次の方法で共有


XPathNavigator.SelectAncestors メソッド

選択基準と一致する現在のノードのすべての祖先要素ノードを選択します。

オーバーロードの一覧

XPathNodeType と一致する現在のノードのすべての祖先要素ノードを選択します。

[Visual Basic] Overloads Public Overridable Function SelectAncestors(XPathNodeType, Boolean) As XPathNodeIterator

[C#] public virtual XPathNodeIterator SelectAncestors(XPathNodeType, bool);

[C++] public: virtual XPathNodeIterator* SelectAncestors(XPathNodeType, bool);

[JScript] public function SelectAncestors(XPathNodeType, Boolean) : XPathNodeIterator;

提供されたローカル名と名前空間 URI を持つ現在のノードのすべての祖先要素ノードを選択します。

[Visual Basic] Overloads Public Overridable Function SelectAncestors(String, String, Boolean) As XPathNodeIterator

[C#] public virtual XPathNodeIterator SelectAncestors(string, string, bool);

[C++] public: virtual XPathNodeIterator* SelectAncestors(String*, String*, bool);

[JScript] public function SelectAncestors(String, String, Boolean) : XPathNodeIterator;

使用例

[Visual Basic, C#, C++] 指定した CD を含むすべての注文を選択する例を次に示します。

[Visual Basic, C#, C++] メモ   ここでは、SelectAncestors のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
Imports System
Imports System.Xml.XPath

public class Sample

  public shared sub Main()

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

    ' Select all cd nodes with the specified ISBN value.
    Dim ni as XPathNodeIterator = nav.Select("//cd[@ISBN='2-3631-4']")

    ' Select all orders containing the selected cd node.
    while (ni.MoveNext())
       Dim nav2 as XPathNavigator = ni.Current
       Dim ni2 as XPathNodeIterator = nav2.SelectAncestors("order", "", false)
       while (ni2.MoveNext())
         Console.WriteLine(ni2.Current.ToString())   
       end while
   end while

  end sub
end class

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

public class Sample
{
  public static void Main()
  {

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

    // Select all cd nodes with the specified ISBN value.
    XPathNodeIterator ni = nav.Select("//cd[@ISBN='2-3631-4']");

    // Select all orders containing the selected cd node.
    while (ni.MoveNext()){
       XPathNavigator nav2 = ni.Current;
       XPathNodeIterator ni2 = nav2.SelectAncestors("order", "", false);
       while (ni2.MoveNext())
         Console.WriteLine(ni2.Current.ToString());   
   }

  }
}

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

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

   // Select all cd nodes with the specified ISBN value.
   XPathNodeIterator * ni = nav -> Select(S"//cd[@ISBN='2-3631-4']");

   // Select all orders containing the selected cd node.
   while (ni -> MoveNext()) 
   {
      XPathNavigator * nav2 = ni -> Current;
      XPathNodeIterator * ni2 = nav2 -> SelectAncestors(S"order", S"", false);
      while (ni2 -> MoveNext())
         Console::WriteLine(ni2 -> Current);   
   }
}

この例では、入力として、 order.xml というファイルを使用しています。

<!--Represents a customer order-->
<order>
  <book ISBN='10-861003-324'>
    <title>The Handmaid's Tale</title>
    <price>19.95</price>
  </book>
  <cd ISBN='2-3631-4'>
    <title>Americana</title>
    <price>16.95</price>
  </cd>
</order>

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

参照

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