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.

另请参阅

适用于