Extensions.Descendants 方法

定义

返回元素集合,其中包含源集合中每个元素和文档的子代元素。

重载

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 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;

类型参数

T

source 中对象的类型,被约束为 XContainer

参数

source
IEnumerable<T>

一个包含源集合的 IEnumerable<T>XContainer

name
XName

要匹配的 XName

返回

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);  

该示例产生下面的输出:

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);  

该示例产生下面的输出:

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) 中的语言集成轴 ,而不是显式使用此轴方法。

此方法使用延迟执行。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Descendants<T>(IEnumerable<T>)

Source:
Extensions.cs
Source:
Extensions.cs
Source:
Extensions.cs

返回元素集合,其中包含源集合中每个元素和文档的子代元素。

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;

类型参数

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);  

该示例产生下面的输出:

<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);  

该示例产生下面的输出:

<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 用户想要检索所有后代,则必须显式使用此轴方法。

此方法使用延迟执行。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0