Extensions.Descendants Metoda

Definicja

Zwraca kolekcję elementów, które zawierają elementy podrzędne każdego elementu i dokumentu w kolekcji źródłowej.

Przeciążenia

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

Zwraca przefiltrowaną kolekcję elementów zawierających elementy podrzędne każdego elementu i dokumentu w kolekcji źródłowej. W kolekcji znajdują się tylko elementy, które mają dopasowanie XName .

Descendants<T>(IEnumerable<T>)

Zwraca kolekcję elementów, które zawierają elementy podrzędne każdego elementu i dokumentu w kolekcji źródłowej.

Uwagi

Użytkownicy języka Visual Basic mogą używać zintegrowanej osi elementów podrzędnych XML do pobierania elementów podrzędnych kolekcji. Jednak zintegrowana oś pobiera tylko elementy podrzędne o określonej nazwie. Jeśli użytkownicy języka Visual Basic chcą pobrać wszystkie elementy podrzędne, muszą jawnie użyć tej metody osi.

Ta metoda używa odroczonego wykonania.

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

Źródło:
Extensions.cs
Źródło:
Extensions.cs
Źródło:
Extensions.cs

Zwraca przefiltrowaną kolekcję elementów zawierających elementy podrzędne każdego elementu i dokumentu w kolekcji źródłowej. W kolekcji znajdują się tylko elementy, które mają dopasowanie 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)

Parametry typu

T

Typ obiektów w elemecie source, ograniczony do XContainer.

Parametry

source
IEnumerable<T>

Element IEnumerable<T> zawierający XContainer kolekcję źródłową.

name
XName

Element XName do dopasowania.

Zwraca

Element IEnumerable<T> zawierający XElement elementy podrzędne każdego elementu i dokumentu w kolekcji źródłowej. W kolekcji znajdują się tylko elementy, które mają dopasowanie XName .

Przykłady

Poniższy przykład pobiera kolekcję dwóch elementów, a następnie pobiera kolekcję wszystkich elementów podrzędnych dwóch elementów, które mają określoną nazwę elementu.

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)  

Ten przykład generuje następujące wyniki:

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

Poniżej przedstawiono ten sam przykład, ale w tym przypadku kod XML znajduje się w przestrzeni nazw. Aby uzyskać więcej informacji, zobacz Praca z przestrzeniami nazw 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  

Ten przykład generuje następujące wyniki:

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

Uwagi

Użytkownicy języka Visual Basic mogą jawnie używać osi zintegrowanych z językiem w języku Visual Basic (LINQ to XML), zamiast jawnie używać tej metody osi.

Ta metoda używa odroczonego wykonania.

Zobacz też

Dotyczy

Descendants<T>(IEnumerable<T>)

Źródło:
Extensions.cs
Źródło:
Extensions.cs
Źródło:
Extensions.cs

Zwraca kolekcję elementów, które zawierają elementy podrzędne każdego elementu i dokumentu w kolekcji źródłowej.

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)

Parametry typu

T

Typ obiektów w elemecie source, ograniczony do XContainer.

Parametry

source
IEnumerable<T>

Element IEnumerable<T> zawierający XContainer kolekcję źródłową.

Zwraca

Element IEnumerable<T> zawierający XElement elementy podrzędne każdego elementu i dokumentu w kolekcji źródłowej.

Przykłady

Poniższy przykład pobiera kolekcję elementów, a następnie używa tej metody osi do pobierania wszystkich elementów malejących każdego elementu w kolekcji elementów.

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  

Ten przykład generuje następujące wyniki:

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

Poniżej przedstawiono ten sam przykład, ale w tym przypadku kod XML znajduje się w przestrzeni nazw. Aby uzyskać więcej informacji, zobacz Praca z przestrzeniami nazw 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  

Ten przykład generuje następujące wyniki:

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

Uwagi

Użytkownicy języka Visual Basic mogą używać zintegrowanej osi elementów podrzędnych XML do pobierania elementów podrzędnych kolekcji. Jednak zintegrowana oś pobiera tylko elementy podrzędne o określonej nazwie. Jeśli użytkownicy języka Visual Basic chcą pobrać wszystkie elementy podrzędne, muszą jawnie użyć tej metody osi.

Ta metoda używa odroczonego wykonania.

Zobacz też

Dotyczy