Extensions.Descendants Extensions.Descendants Extensions.Descendants Extensions.Descendants Method

定義

傳回包含來源集合中每個項目和文件之子代項目的項目集合。Returns a collection of elements that contains the descendant elements of every element and document in the source collection.

多載

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

傳回已篩選的項目集合,其中包含來源集合中每個項目和文件的子代項目。Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. 集合中只會包含具有相符之 XName 的項目。Only elements that have a matching XName are included in the collection.

Descendants<T>(IEnumerable<T>) Descendants<T>(IEnumerable<T>) Descendants<T>(IEnumerable<T>) Descendants<T>(IEnumerable<T>)

傳回包含來源集合中每個項目和文件之子代項目的項目集合。Returns a collection of elements that contains the descendant elements of every element and document in the source collection.

備註

Visual Basic 使用者可以使用整合式的 XML 子代軸,來擷取集合的子系的項目。Visual Basic users can use the integrated XML descendant axis to retrieve the descendant elements of a collection. 不過,整合式的軸只會擷取具有指定名稱的子代。However, the integrated axis only retrieves descendants with a specified name. 如果 Visual Basic 使用者想要擷取所有子系,則它們必須使用此座標軸方法明確。If Visual Basic users want to retrieve all descendants, then they must use this axis method explicitly.

這個方法會使用延後的執行。This method uses deferred execution.

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

傳回已篩選的項目集合,其中包含來源集合中每個項目和文件的子代項目。Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. 集合中只會包含具有相符之 XName 的項目。Only elements that have a matching XName are included in the collection.

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;
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 中物件的型別,限制為 XContainerThe type of the objects in source, constrained to XContainer.

參數

source
IEnumerable<T> IEnumerable<T> IEnumerable<T> IEnumerable<T>

IEnumerable<T>XContainer,其中包含來源集合。An IEnumerable<T> of XContainer that contains the source collection.

name
XName XName XName XName

要比對的 XNameThe XName to match.

傳回

IEnumerable<T>XElement,其中包含來源集合中每個項目和文件的子代項目。An IEnumerable<T> of XElement that contains the descendant elements of every element and document in the source collection. 集合中只會包含具有相符之 XName 的項目。Only elements that have a matching XName are included in the collection.

範例

下列範例會擷取兩個元素的集合,並接著會擷取具有指定的項目名稱的兩個元素的所有下階的集合。The following example retrieves a collection of two elements, and then retrieves a collection of all descendants of the two elements that have the specified element name.

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 example produces the following output:

This is some text where all of the text nodes must be concatenated. This is a second sentence.  

以下是相同的範例,但在此情況下的 XML 位於命名空間。The following is the same example, but in this case the XML is in a namespace. 如需詳細資訊,請參閱 < 處理 XML 命名空間For more information, see Working with XML Namespaces.

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 example produces the following output:

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) 中的 Language-Integrated 軸而不是明確地使用此座標軸方法。Visual Basic users can use the Language-Integrated Axes in Visual Basic (LINQ to XML) instead of using this axis method explicitly.

這個方法會使用延後的執行。This method uses deferred execution.

另請參閱

Descendants<T>(IEnumerable<T>) Descendants<T>(IEnumerable<T>) Descendants<T>(IEnumerable<T>) Descendants<T>(IEnumerable<T>)

傳回包含來源集合中每個項目和文件之子代項目的項目集合。Returns a collection of elements that contains the descendant elements of every element and document in the source collection.

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;
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 中物件的型別,限制為 XContainerThe type of the objects in source, constrained to XContainer.

參數

source
IEnumerable<T> IEnumerable<T> IEnumerable<T> IEnumerable<T>

IEnumerable<T>XContainer,其中包含來源集合。An IEnumerable<T> of XContainer that contains the source collection.

傳回

IEnumerable<T>XElement,其中包含來源集合中每個項目和文件的子代項目。An IEnumerable<T> of XElement that contains the descendant elements of every element and document in the source collection.

範例

下列範例擷取的項目集合,然後使用此座標軸方法來擷取項目的集合中每個項目的所有子代項目。The following example retrieves a collection of elements, and then uses this axis method to retrieve all descendent elements of every item in the collection of elements.

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  

這個範例會產生下列輸出:This example produces the following output:

<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 位於命名空間。The following is the same example, but in this case the XML is in a namespace. 如需詳細資訊,請參閱 < 處理 XML 命名空間For more information, see Working with XML Namespaces.

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  

這個範例會產生下列輸出:This example produces the following output:

<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 users can use the integrated XML descendant axis to retrieve the descendant elements of a collection. 不過,整合式的軸只會擷取具有指定名稱的子代。However, the integrated axis only retrieves descendants with a specified name. 如果 Visual Basic 使用者想要擷取所有子系,則它們必須使用此座標軸方法明確。If Visual Basic users want to retrieve all descendants, then they must use this axis method explicitly.

這個方法會使用延後的執行。This method uses deferred execution.

另請參閱

適用於