XNode.Ancestors 方法

定義

傳回這個節點之上階元素的集合。Returns a collection of the ancestor elements of this node.

多載

Ancestors()

傳回這個節點之上階元素的集合。Returns a collection of the ancestor elements of this node.

Ancestors(XName)

傳回這個節點的上階元素之篩選的集合。Returns a filtered collection of the ancestor elements of this node. 集合中只會包含具有相符之 XName 的項目。Only elements that have a matching XName are included in the collection.

備註

您可以選擇性地指定節點名稱,以篩選具有特定名稱的祖系元素。Optionally a node name can be specified to filter for ancestor elements with a specific name.

所傳回集合中之節點的順序為反向文件順序。The nodes in the returned collection are in reverse document order.

這個方法會使用延後的執行。This method uses deferred execution.

Ancestors()

傳回這個節點之上階元素的集合。Returns a collection of the ancestor elements of this node.

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>,屬於這個節點之祖系項目的 XElementAn IEnumerable<T> of XElement of the ancestor elements of this node.

範例

下列範例會使用這個方法來列舉節點的祖系。The following example uses this method to enumerate the ancestors of a node.

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  

這個範例會產生下列輸出:This example produces the following output:

Child  
Root  

備註

這個方法不會在結果中傳回本身。This method does not return itself in the results.

所傳回集合中之節點的順序為反向文件順序。The nodes in the returned collection are in reverse document order.

這個方法會使用延後的執行。This method uses deferred execution.

另請參閱

Ancestors(XName)

傳回這個節點的上階元素之篩選的集合。Returns a filtered collection of the ancestor elements of this node. 集合中只會包含具有相符之 XName 的項目。Only elements that have a matching XName are included in the collection.

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

要比對的 XNameThe XName to match.

傳回

IEnumerable<T>,屬於這個節點之祖系項目的 XElementAn IEnumerable<T> of XElement of the ancestor elements of this node. 集合中只會包含具有相符之 XName 的項目。Only elements that have a matching XName are included in the collection.

所傳回集合中之節點的順序為反向文件順序。The nodes in the returned collection are in reverse document order.

這個方法會使用延後的執行。This method uses deferred execution.

範例

下列範例會使用這個方法。The following example uses this method.

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  

這個範例會產生下列輸出:This example produces the following output:

Child  

備註

這個方法不會在結果中傳回本身。This method will not return itself in the results.

另請參閱

適用於