XNode.Ancestors 方法

定義

傳回這個節點之上階元素的集合。

多載

Ancestors()

傳回這個節點之上階元素的集合。

Ancestors(XName)

傳回這個節點的上階元素之篩選的集合。 集合中只會包含具有相符之 XName 的項目。

備註

您可以選擇性地指定節點名稱,以篩選具有特定名稱的上階專案。

所傳回集合中之節點的順序為反向文件順序。

這個方法會使用延後的執行。

Ancestors()

Source:
XNode.cs
Source:
XNode.cs
Source:
XNode.cs

傳回這個節點之上階元素的集合。

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)

Source:
XNode.cs
Source:
XNode.cs
Source:
XNode.cs

傳回這個節點的上階元素之篩選的集合。 集合中只會包含具有相符之 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  

備註

這個方法不會在結果中傳回本身。

另請參閱

適用於