XElement.DescendantsAndSelf 方法

定义

返回元素的集合,而这些元素包含此元素以及此元素的所有子代元素,并按文档顺序排列它们。Returns a collection of elements that contain this element, and all descendant elements of this element, in document order.

重载

DescendantsAndSelf(XName)

返回经过筛选的元素集合,这些元素包含此元素以及此元素的所有子代元素,并按文档顺序排列它们。Returns a filtered collection of elements that contain this element, and all descendant elements of this element, in document order. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

DescendantsAndSelf()

返回元素的集合,而这些元素包含此元素以及此元素的所有子代元素,并按文档顺序排列它们。Returns a collection of elements that contain this element, and all descendant elements of this element, in document order.

注解

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

DescendantsAndSelf(XName)

返回经过筛选的元素集合,这些元素包含此元素以及此元素的所有子代元素,并按文档顺序排列它们。Returns a filtered collection of elements that contain this element, and all descendant elements of this element, in document order. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

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

参数

name
XName

要匹配的 XNameThe XName to match.

返回

元素(其中包含此元素以及此元素的所有子代元素,并按文档顺序排列)的 IEnumerable<T>XElementAn IEnumerable<T> of XElement that contain this element, and all descendant elements of this element, in document order. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

示例

下面的示例创建一个 XML 树,然后使用此树。The following example creates an XML tree, and then uses this .

XElement xmlTree = new XElement("Root",  
    new XAttribute("Att1", "AttributeContent"),  
    new XElement("Child",  
        new XText("Some text"),  
        new XElement("GrandChild", "element content")  
    )  
);  
IEnumerable<XElement> das = xmlTree.DescendantsAndSelf("Child");  
foreach (XElement el in das)  
    Console.WriteLine(el.Name);  
Dim xmlTree As XElement = _  
    <Root Att1="AttributeContent">  
        <Child>Some text  
            <GrandChild>element content</GrandChild>  
        </Child>  
    </Root>  
  
Dim das As IEnumerable(Of XElement) = xmlTree.DescendantsAndSelf("Child")  
  
For Each el In das  
    Console.WriteLine(el.Name)  
Next  

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

Child  

注解

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

另请参阅

DescendantsAndSelf()

返回元素的集合,而这些元素包含此元素以及此元素的所有子代元素,并按文档顺序排列它们。Returns a collection of elements that contain this element, and all descendant elements of this element, in document order.

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

返回

元素(其中包含此元素以及此元素的所有子代元素,并按文档顺序排列)的 IEnumerable<T>XElementAn IEnumerable<T> of XElement of elements that contain this element, and all descendant elements of this element, in document order.

示例

下面的示例创建一个 XML 树,然后使用此树。The following example creates an XML tree, and then uses this .

XElement xmlTree = new XElement("Root",  
    new XAttribute("Att1", "AttributeContent"),  
    new XElement("Child",  
        new XText("Some text"),  
        new XElement("GrandChild", "element content")  
    )  
);  
IEnumerable<XElement> das =  
    from el in xmlTree.DescendantsAndSelf()  
    select el;  
foreach (XElement el in das)  
    Console.WriteLine(el.Name);  
Dim xmlTree As XElement = _  
    <Root Att1="AttributeContent">  
        <Child>Some text  
            <GrandChild>element content</GrandChild>  
        </Child>  
    </Root>  
  
Dim das As IEnumerable(Of XElement) = _  
    From el In xmlTree.DescendantsAndSelf() _  
    Select el  
  
For Each el In das  
    Console.WriteLine(el.Name)  
Next  

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

Root  
Child  
GrandChild  

注解

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

另请参阅

适用于