XNode.Ancestors メソッド

定義

このノードの先祖要素のコレクションを返します。

オーバーロード

Ancestors()

このノードの先祖要素のコレクションを返します。

Ancestors(XName)

このノードの先祖要素のフィルター処理されたコレクションを返します。 一致する XName を持つ要素のみがコレクションに含められます。

注釈

必要に応じて、ノード名を指定して、特定の名前を持つ先祖要素をフィルター処理できます。

返されたコレクションのノードは、ドキュメントの逆順になっています。

このメソッドは遅延実行を使用します。

Ancestors()

このノードの先祖要素のコレクションを返します。

public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Ancestors();
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Ancestors ();
member this.Ancestors : unit -> seq<System.Xml.Linq.XElement>
Public Function Ancestors () As IEnumerable(Of XElement)

戻り値

このノードの先祖要素の IEnumerable<T>XElement

次の例では、このメソッドを使用してノードの先祖を列挙します。

XElement xmlTree = new XElement("Root",  
    new XElement("Child",   
        new XElement("GrandChild", "content")  
    )  
);  
IEnumerable<XElement> grandChild = xmlTree.Descendants("GrandChild");  
foreach (XElement el in grandChild.Ancestors())  
    Console.WriteLine(el.Name);  
Dim xmlTree As XElement = _   
        <Root>  
            <Child>  
                <GrandChild>content</GrandChild>  
            </Child>  
        </Root>  

Dim grandChild As IEnumerable(Of XElement) = xmlTree...<GrandChild>  
For Each el In grandChild.Ancestors()  
    Console.WriteLine(el.Name)  
Next  

この例を実行すると、次の出力が生成されます。

Child  
Root  

注釈

このメソッドは、結果にそれ自体を返しません。

返されたコレクションのノードは、ドキュメントの逆順になっています。

このメソッドは遅延実行を使用します。

こちらもご覧ください

適用対象

Ancestors(XName)

このノードの先祖要素のフィルター処理されたコレクションを返します。 一致する XName を持つ要素のみがコレクションに含められます。

public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Ancestors(System::Xml::Linq::XName ^ name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Ancestors (System.Xml.Linq.XName name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Ancestors (System.Xml.Linq.XName? name);
member this.Ancestors : System.Xml.Linq.XName -> seq<System.Xml.Linq.XElement>
Public Function Ancestors (name As XName) As IEnumerable(Of XElement)

パラメーター

name
XName

照合する XName

戻り値

このノードの先祖要素の IEnumerable<T>XElement。 一致する XName を持つ要素のみがコレクションに含められます。

返されたコレクションのノードは、ドキュメントの逆順になっています。

このメソッドは遅延実行を使用します。

次の例では、このメソッドを使用します。

XElement xmlTree = new XElement("Root",  
    new XElement("Child",   
        new XElement("GrandChild", "content")  
    )  
);  
IEnumerable<XElement> grandChild = xmlTree.Descendants("GrandChild");  
foreach (XElement el in grandChild.Ancestors("Child"))  
    Console.WriteLine(el.Name);  
Dim xmlTree As XElement = _   
        <Root>  
            <Child>  
                <GrandChild>content</GrandChild>  
            </Child>  
        </Root>  

Dim grandChild As IEnumerable(Of XElement) = xmlTree...<GrandChild>  
For Each el In grandChild.Ancestors("Child")  
    Console.WriteLine(el.Name)  
Next  

この例を実行すると、次の出力が生成されます。

Child  

注釈

このメソッドは結果にそれ自体を返しません。

こちらもご覧ください

適用対象