Extensions.Descendants 方法

定義

傳回包含來源集合中每個項目和文件之子代項目的項目集合。

多載

Descendants<T>(IEnumerable<T>, XName)

傳回已篩選的項目集合,其中包含來源集合中每個項目和文件的子代項目。 集合中只會包含具有相符之 XName 的項目。

Descendants<T>(IEnumerable<T>)

傳回包含來源集合中每個項目和文件之子代項目的項目集合。

備註

Visual Basic 使用者可以使用整合式 XML 子代座標軸來擷取集合的子代專案。 不過,整合式座標軸只會擷取具有指定名稱的子系。 如果 Visual Basic 使用者想要擷取所有子系,則必須明確使用此軸方法。

這個方法會使用延後的執行。

Descendants<T>(IEnumerable<T>, XName)

來源:
Extensions.cs
來源:
Extensions.cs
來源:
Extensions.cs

傳回已篩選的項目集合,其中包含來源集合中每個項目和文件的子代項目。 集合中只會包含具有相符之 XName 的項目。

C#
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;
C#
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 的項目。

範例

下列範例會擷取兩個專案的集合,然後擷取具有指定專案名稱之兩個元素之所有子系的集合。

C#
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 命名空間

C#
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 中使用 Language-Integrated Axes (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>)

來源:
Extensions.cs
來源:
Extensions.cs
來源:
Extensions.cs

傳回包含來源集合中每個項目和文件之子代項目的項目集合。

C#
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;
C#
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,其中包含來源集合中每個項目和文件的子代項目。

範例

下列範例會擷取專案集合,然後使用這個軸方法來擷取專案集合中每個專案的子代專案。

C#
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 命名空間

C#
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