Extensions.Descendants 方法 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回元素集合,其中包含源集合中每个元素和文档的子代元素。
重载
| Descendants<T>(IEnumerable<T>, XName) | 
						 返回经过筛选的元素集合,其中包含源集合中每个元素和文档的子代元素。 集合中仅包括具有匹配 XName 的元素。  | 
        	
| Descendants<T>(IEnumerable<T>) | 
						 返回元素集合,其中包含源集合中每个元素和文档的子代元素。  | 
        	
注解
Visual Basic 用户可以使用集成的 XML 子代轴来检索集合的后代元素。 但是,集成轴仅检索具有指定名称的后代。 如果 Visual Basic 用户想要检索所有后代,则必须显式使用此轴方法。
此方法使用延迟执行。
Descendants<T>(IEnumerable<T>, XName)
- Source:
 - Extensions.cs
 
- Source:
 - Extensions.cs
 
- Source:
 - Extensions.cs
 
返回经过筛选的元素集合,其中包含源集合中每个元素和文档的子代元素。 集合中仅包括具有匹配 XName 的元素。
public:
generic <typename T>
 where T : System::Xml::Linq::XContainer[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Descendants(System::Collections::Generic::IEnumerable<T> ^ source, System::Xml::Linq::XName ^ name);
	public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants<T> (this System.Collections.Generic.IEnumerable<T> source, System.Xml.Linq.XName name) where T : System.Xml.Linq.XContainer;
	public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants<T> (this System.Collections.Generic.IEnumerable<T?> source, System.Xml.Linq.XName? name) where T : System.Xml.Linq.XContainer;
	static member Descendants : seq<'T (requires 'T :> System.Xml.Linq.XContainer)> * System.Xml.Linq.XName -> seq<System.Xml.Linq.XElement> (requires 'T :> System.Xml.Linq.XContainer)
	<Extension()>
Public Function Descendants(Of T As XContainer) (source As IEnumerable(Of T), name As XName) As IEnumerable(Of XElement)
    类型参数
- T
 
              source 中对象的类型,被约束为 XContainer。
参数
- source
 - IEnumerable<T>
 
一个包含源集合的 IEnumerable<T> 的 XContainer。
返回
IEnumerable<T> 的 XElement,其中包含源集合中每个元素和文档的子代元素。 集合中仅包括具有匹配 XName 的元素。
示例
下面的示例检索两个元素的集合,然后检索具有指定元素名称的两个元素的所有后代的集合。
XElement xmlTree = XElement.Parse(  
@"<Root>  
    <Para>  
        <t>This is some text </t>  
        <b>  
            <t>where</t>  
        </b>  
        <t> all of the text nodes must be concatenated. </t>  
    </Para>  
    <Para>  
        <t>This is a second sentence.</t>  
    </Para>  
</Root>");  
string str =  
    (from el in xmlTree.Elements("Para").Descendants("t")  
    select (string)el)  
    .Aggregate(new StringBuilder(),  
        (sb, i) => sb.Append(i),  
        sb => sb.ToString());  
Console.WriteLine(str);  
Dim xmlTree As XElement = _  
    <Root>  
        <Para>  
            <t>This is some text </t>  
            <b>  
                <t>where</t>  
            </b>  
            <t> all of the text nodes must be concatenated. </t>  
        </Para>  
        <Para>  
            <t>This is a second sentence.</t>  
        </Para>  
    </Root>  
Dim str As String = _  
    ( _  
        From el In xmlTree.<Para>...<t> _  
        Select CStr(el) _  
    ) _  
    .Aggregate(New StringBuilder(), _  
               Function(ByVal sb, ByVal i) sb.Append(i), _  
               Function(ByVal sb) sb.ToString())  
Console.WriteLine(str)  
该示例产生下面的输出:
This is some text where all of the text nodes must be concatenated. This is a second sentence.  
下面是相同的示例,但在本例中,XML 位于 命名空间中。 有关详细信息,请参阅 使用 XML 命名空间。
XNamespace aw = "http://www.adventure-works.com";  
XElement xmlTree = XElement.Parse(  
@"<Root xmlns='http://www.adventure-works.com'>  
    <Para>  
        <t>This is some text </t>  
        <b>  
            <t>where</t>  
        </b>  
        <t> all of the text nodes must be concatenated. </t>  
    </Para>  
    <Para>  
        <t>This is a second sentence.</t>  
    </Para>  
</Root>");  
string str =  
    (from el in xmlTree.Elements(aw + "Para").Descendants(aw + "t")  
     select (string)el)  
    .Aggregate(new StringBuilder(),  
        (sb, i) => sb.Append(i),  
        sb => sb.ToString());  
Console.WriteLine(str);  
Imports <xmlns="http://www.adventure-works.com">  
Module Module1  
    Sub Main()  
        Dim xmlTree As XElement = _  
            <Root>  
                <Para>  
                    <t>This is some text </t>  
                    <b>  
                        <t>where</t>  
                    </b>  
                    <t> all of the text nodes must be concatenated. </t>  
                </Para>  
                <Para>  
                    <t>This is a second sentence.</t>  
                </Para>  
            </Root>  
        Dim str As String = _  
            ( _  
                From el In xmlTree.<Para>...<t> _  
                Select CStr(el) _  
            ) _  
            .Aggregate(New StringBuilder(), _  
                       Function(sb, i) sb.Append(i), _  
                       Function(sb) sb.ToString())  
        Console.WriteLine(str)  
    End Sub  
End Module  
该示例产生下面的输出:
This is some text where all of the text nodes must be concatenated. This is a second sentence.  
    	注解
Visual Basic 用户可以使用 Visual Basic (LINQ to XML) 中的语言集成轴 ,而不是显式使用此轴方法。
此方法使用延迟执行。
另请参阅
- DescendantNodesAndSelf()
 - DescendantsAndSelf()
 - DescendantNodes()
 - Descendants()
 - DescendantNodes<T>(IEnumerable<T>)
 - DescendantsAndSelf
 - Nodes<T>(IEnumerable<T>)
 - LINQ to XML 概述
 
适用于
Descendants<T>(IEnumerable<T>)
- Source:
 - Extensions.cs
 
- Source:
 - Extensions.cs
 
- Source:
 - Extensions.cs
 
返回元素集合,其中包含源集合中每个元素和文档的子代元素。
public:
generic <typename T>
 where T : System::Xml::Linq::XContainer[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Descendants(System::Collections::Generic::IEnumerable<T> ^ source);
	public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants<T> (this System.Collections.Generic.IEnumerable<T> source) where T : System.Xml.Linq.XContainer;
	public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants<T> (this System.Collections.Generic.IEnumerable<T?> source) where T : System.Xml.Linq.XContainer;
	static member Descendants : seq<'T (requires 'T :> System.Xml.Linq.XContainer)> -> seq<System.Xml.Linq.XElement> (requires 'T :> System.Xml.Linq.XContainer)
	<Extension()>
Public Function Descendants(Of T As XContainer) (source As IEnumerable(Of T)) As IEnumerable(Of XElement)
    类型参数
- T
 
              source 中对象的类型,被约束为 XContainer。
参数
- source
 - IEnumerable<T>
 
一个包含源集合的 IEnumerable<T> 的 XContainer。
返回
IEnumerable<T> 的 XElement,其中包含源集合中每个元素和文档的子代元素。
示例
以下示例检索元素的集合,然后使用此轴方法检索元素集合中每个项的所有后代元素。
XElement xmlTree = XElement.Parse(  
@"<Root>  
    <Para>  
        <t>This is some text </t>  
        <b>  
            <t>where</t>  
        </b>  
        <t> all of the nodes must be concatenated. </t>  
    </Para>  
    <Para>  
        <t>This is a second sentence.</t>  
    </Para>  
</Root>");  
IEnumerable<XElement> elList =  
    from el in xmlTree.Elements("Para").Descendants()  
    select el;  
foreach (XElement el in elList)  
    Console.WriteLine(el);  
Dim xmlTree As XElement = _  
    <Root>  
        <Para>  
            <t>This is some text </t>  
            <b>  
                <t>where</t>  
            </b>  
            <t> all of the nodes must be concatenated. </t>  
        </Para>  
        <Para>  
            <t>This is a second sentence.</t>  
        </Para>  
    </Root>  
Dim elList = From el In xmlTree.<Para>.Descendants _  
                        Select el  
For Each el As XElement In elList  
    Console.WriteLine(el)  
Next  
该示例产生下面的输出:
<t>This is some text </t>  
<b>  
  <t>where</t>  
</b>  
<t>where</t>  
<t> all of the nodes must be concatenated. </t>  
<t>This is a second sentence.</t>  
下面是相同的示例,但在本例中,XML 位于 命名空间中。 有关详细信息,请参阅 使用 XML 命名空间。
XNamespace aw = "http://www.adventure-works.com";  
XElement xmlTree = XElement.Parse(  
@"<Root xmlns='http://www.adventure-works.com'>  
    <Para>  
        <t>This is some text </t>  
        <b>  
            <t>where</t>  
        </b>  
        <t> all of the nodes must be concatenated. </t>  
    </Para>  
    <Para>  
        <t>This is a second sentence.</t>  
    </Para>  
</Root>");  
IEnumerable<XElement> elList =  
    from el in xmlTree.Elements(aw + "Para").Descendants()  
    select el;  
foreach (XElement el in elList)  
    Console.WriteLine(el);  
Imports <xmlns="http://www.adventure-works.com">  
Module Module1  
    Sub Main()  
        Dim xmlTree As XElement = _  
            <Root>  
                <Para>  
                    <t>This is some text </t>  
                    <b>  
                        <t>where</t>  
                    </b>  
                    <t> all of the nodes must be concatenated. </t>  
                </Para>  
                <Para>  
                    <t>This is a second sentence.</t>  
                </Para>  
            </Root>  
        Dim elList = From el In xmlTree.<Para>.Descendants _  
                                Select el  
        For Each el As XElement In elList  
            Console.WriteLine(el)  
        Next  
    End Sub  
End Module  
该示例产生下面的输出:
<t xmlns="http://www.adventure-works.com">This is some text </t>  
<b xmlns="http://www.adventure-works.com">  
  <t>where</t>  
</b>  
<t xmlns="http://www.adventure-works.com">where</t>  
<t xmlns="http://www.adventure-works.com"> all of the nodes must be concatenated. </t>  
<t xmlns="http://www.adventure-works.com">This is a second sentence.</t>  
    	注解
Visual Basic 用户可以使用集成的 XML 子代轴来检索集合的后代元素。 但是,集成轴仅检索具有指定名称的后代。 如果 Visual Basic 用户想要检索所有后代,则必须显式使用此轴方法。
此方法使用延迟执行。
另请参阅
- DescendantNodesAndSelf()
 - DescendantsAndSelf()
 - DescendantNodes()
 - Descendants()
 - DescendantNodes<T>(IEnumerable<T>)
 - DescendantsAndSelf
 - Nodes<T>(IEnumerable<T>)
 - LINQ to XML 概述