XElement.AncestorsAndSelf 方法

定义

返回元素集合,其中包含此元素及其上级。Returns a collection of elements that contain this element, and the ancestors of this element.

重载

AncestorsAndSelf()

返回元素集合,其中包含此元素及其上级。Returns a collection of elements that contain this element, and the ancestors of this element.

AncestorsAndSelf(XName)

返回经过筛选的元素集合,其中包含此元素及其上级。Returns a filtered collection of elements that contain this element, and the ancestors of this element. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

注解

返回的集合中的元素的顺序与文档顺序相反。The elements in the returned collection are in reverse document order.

此方法使用延迟执行。This method uses deferred execution.

AncestorsAndSelf()

返回元素集合,其中包含此元素及其上级。Returns a collection of elements that contain this element, and the ancestors of this element.

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

返回

元素(其中包含此元素及其上级)的 IEnumerable<T>XElementAn IEnumerable<T> of XElement of elements that contain this element, and the ancestors of this element.

示例

下面的示例创建一个 XML 树。The following example creates an XML tree. 然后,它将查找 GrandChild 元素,然后打印其上级。It then finds the GrandChild element, and then prints the ancestors of it.

XElement xmlTree = new XElement("Root",  
    new XElement("Child",  
        new XElement("GrandChild", "element content")  
    )  
);  
XElement gc = xmlTree.Element("Child").Element("GrandChild");  
IEnumerable<XElement> aas =  
    from el in gc.AncestorsAndSelf()  
    select el;  
foreach (XElement el in aas)  
    Console.WriteLine(el.Name);  
Dim xmlTree As XElement = _   
    <Root>  
        <Child>  
            <GrandChild>element content</GrandChild>  
        </Child>  
    </Root>  
  
Dim GC As XElement = xmlTree.<Child>.<GrandChild>(0)  
  
Dim aas As IEnumerable(Of XElement) = _  
    From el In GC.AncestorsAndSelf() _  
    Select el  
  
For Each el In aas  
    Console.WriteLine(el.Name)  
Next  

该示例产生下面的输出:This example produces the following output:

GrandChild  
Child  
Root  

注解

返回的集合中的元素的顺序与文档顺序相反。The elements in the returned collection are in reverse document order.

此方法使用延迟执行。This method uses deferred execution.

另请参阅

AncestorsAndSelf(XName)

返回经过筛选的元素集合,其中包含此元素及其上级。Returns a filtered collection of elements that contain this element, and the ancestors of this element. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

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

参数

name
XName

要匹配的 XNameThe XName to match.

返回

IEnumerable<T>XElement,其中包含此元素及其上级。An IEnumerable<T> of XElement that contain this element, and the ancestors of this element. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

示例

下面的示例使用此。The following example uses this .

XElement xmlTree = new XElement("Root",  
    new XElement("Child",  
        new XElement("GrandChild", "element content")  
    )  
);  
XElement gc = xmlTree.Element("Child").Element("GrandChild");  
IEnumerable<XElement> aas = gc.AncestorsAndSelf("Child");  
foreach (XElement el in aas)  
    Console.WriteLine(el.Name);  
Dim xmlTree As XElement = _   
    <Root>  
        <Child>  
            <GrandChild>element content</GrandChild>  
        </Child>  
    </Root>  
  
Dim GC As XElement = xmlTree.<Child>.<GrandChild>(0)  
Dim aas As IEnumerable(Of XElement) = GC.AncestorsAndSelf("Child")  
For Each el In aas  
    Console.WriteLine(el.Name)  
Next  

该示例产生下面的输出:This example produces the following output:

Child  

注解

返回的集合中的元素的顺序与文档顺序相反。The elements in the returned collection are in reverse document order.

此方法使用延迟执行。This method uses deferred execution.

另请参阅

适用于