XContainer.Descendants 方法 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
按文档顺序返回此文档或元素的子代元素集合。
重载
| Descendants() | 按文档顺序返回此文档或元素的子代元素集合。 | 
| Descendants(XName) | 按文档顺序返回此文档或元素的已筛选的子代元素集合。 集合中仅包括具有匹配 XName 的元素。 | 
注解
此方法使用延迟执行。
Descendants()
- Source:
- XContainer.cs
- Source:
- XContainer.cs
- Source:
- XContainer.cs
- Source:
- XContainer.cs
按文档顺序返回此文档或元素的子代元素集合。
public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Descendants();public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants();member this.Descendants : unit -> seq<System.Xml.Linq.XElement>Public Function Descendants () As IEnumerable(Of XElement)返回
XElement 的 IEnumerable<T>,其中包含 XContainer 的子代元素。
示例
以下示例创建一个 XML 树,然后使用此轴方法检索后代。
XElement xmlTree = new XElement("Root",  
    new XAttribute("Att1", "AttributeContent"),  
    new XElement("Child",  
        new XText("Some text"),  
        new XElement("GrandChild", "element content")  
    )  
);  
IEnumerable<XElement> de =  
    from el in xmlTree.Descendants()  
    select el;  
foreach (XElement el in de)  
    Console.WriteLine(el.Name);  
' Attributes are not nodes, so will not be returned by DescendantNodes.  
Dim xmlTree As XElement = _  
    <Root Att1="AttributeContent">  
        <Child>Some text  
            <GrandChild>element content</GrandChild>  
        </Child>  
    </Root>  
Dim de = From el In xmlTree.Descendants _  
         Select el  
For Each el In de  
    Console.WriteLine(el.Name)  
Next  
该示例产生下面的输出:
Child  
GrandChild  
注解
请注意,此方法不会在生成的 IEnumerable<T>中返回自身。 查看 DescendantsAndSelf 是否需要在结果中包含当前 XElement 。
此方法使用延迟执行。
另请参阅
适用于
Descendants(XName)
- Source:
- XContainer.cs
- Source:
- XContainer.cs
- Source:
- XContainer.cs
- Source:
- XContainer.cs
按文档顺序返回此文档或元素的已筛选的子代元素集合。 集合中仅包括具有匹配 XName 的元素。
public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Descendants(System::Xml::Linq::XName ^ name);public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants(System.Xml.Linq.XName name);public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants(System.Xml.Linq.XName? name);member this.Descendants : System.Xml.Linq.XName -> seq<System.Xml.Linq.XElement>Public Function Descendants (name As XName) As IEnumerable(Of XElement)参数
返回
XElement 的 IEnumerable<T>,其中包含与指定 XName 相匹配的 XContainer 的子代元素。
示例
以下示例打印元素的所有后代。
// Attributes are not nodes, so will not be returned by DescendantNodes.  
XElement xmlTree = new XElement("Root",  
    new XAttribute("Att1", "AttributeContent"),  
    new XElement("Child",  
        new XText("Some text"),  
        new XElement("GrandChild", "element content")  
    )  
);  
IEnumerable<XElement> de =  
    from el in xmlTree.Descendants("Child")  
    select el;  
foreach (XElement el in de)  
    Console.WriteLine(el.Name);  
' Attributes are not nodes, so will not be returned by the descendants axis.  
Dim xmlTree As XElement = _   
    <Root Att1="AttributeContent">  
         <Child>Some text  
             <GrandChild>element content</GrandChild>  
         </Child>  
     </Root>  
Dim de = From el In xmlTree...<Child> _  
         Select el  
For Each el In de  
    Console.WriteLine(el.Name)  
Next  
该示例产生下面的输出:
Child  
下面是相同的示例,但在本例中,XML 位于 命名空间中。 有关详细信息,请参阅 使用 XML 命名空间。
// Attributes are not nodes, so will not be returned by DescendantNodes.  
XNamespace aw = "http://www.adventure-works.com";  
XElement xmlTree = new XElement(aw + "Root",  
    new XAttribute(aw + "Att1", "AttributeContent"),  
    new XElement(aw + "Child",  
        new XText("Some text"),  
        new XElement(aw + "GrandChild", "element content")  
    )  
);  
IEnumerable<XElement> de =  
    from el in xmlTree.Descendants(aw + "Child")  
    select el;  
foreach (XElement el in de)  
    Console.WriteLine(el.Name);  
Imports <xmlns:aw = "http://www.adventure-works.com">  
Module Module1  
    Sub Main()  
        ' Attributes are not nodes, so will not be returned by the descendants axis.  
        Dim xmlTree As XElement = _   
            <aw:Root aw:Att1="AttributeContent">  
                 <aw:Child>Some text  
                     <aw:GrandChild>element content</aw:GrandChild>  
                 </aw:Child>  
             </aw:Root>  
        Dim de = From el In xmlTree...<aw:Child> _  
                 Select el  
        For Each el In de  
            Console.WriteLine(el.Name)  
        Next  
    End Sub  
End Module  
该示例产生下面的输出:
{http://www.adventure-works.com}Child  
注解
此方法使用延迟执行。