XContainer.Descendants Metod

Definition

Returnerar en samling underordnade element för det här dokumentet eller elementet i dokumentordning.

Överlagringar

Name Description
Descendants()

Returnerar en samling underordnade element för det här dokumentet eller elementet i dokumentordning.

Descendants(XName)

Returnerar en filtrerad samling av underordnade element för det här dokumentet eller elementet i dokumentordning. Endast element som har matchning XName ingår i samlingen.

Kommentarer

Den här metoden använder uppskjuten körning.

Descendants()

Källa:
XContainer.cs
Källa:
XContainer.cs
Källa:
XContainer.cs
Källa:
XContainer.cs
Källa:
XContainer.cs

Returnerar en samling underordnade element för det här dokumentet eller elementet i dokumentordning.

public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Descendants();
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants();
member this.Descendants : unit -> seq<System.Xml.Linq.XElement>
Public Function Descendants () As IEnumerable(Of XElement)

Returer

En IEnumerable<T> av XElement som innehåller underordnade element i XContainer.

Exempel

I följande exempel skapas ett XML-träd och den här axelmetoden används sedan för att hämta underordnade.

XElement xmlTree = new XElement("Root",
    new XAttribute("Att1", "AttributeContent"),
    new XElement("Child",
        new XText("Some text"),
        new XElement("GrandChild", "element content")
    )
);
IEnumerable<XElement> de =
    from el in xmlTree.Descendants()
    select el;
foreach (XElement el in de)
    Console.WriteLine(el.Name);
' Attributes are not nodes, so will not be returned by DescendantNodes.
Dim xmlTree As XElement = _
    <Root Att1="AttributeContent">
        <Child>Some text
            <GrandChild>element content</GrandChild>
        </Child>
    </Root>
Dim de = From el In xmlTree.Descendants _
         Select el

For Each el In de
    Console.WriteLine(el.Name)
Next

Det här exemplet genererar följande utdata:

Child
GrandChild

Kommentarer

Observera att den här metoden inte returnerar sig själv i den resulterande IEnumerable<T>. Se DescendantsAndSelf om du behöver inkludera aktuell i XElement resultatet.

Den här metoden använder uppskjuten körning.

Se även

Gäller för

Descendants(XName)

Källa:
XContainer.cs
Källa:
XContainer.cs
Källa:
XContainer.cs
Källa:
XContainer.cs
Källa:
XContainer.cs

Returnerar en filtrerad samling av underordnade element för det här dokumentet eller elementet i dokumentordning. Endast element som har matchning XName ingår i samlingen.

public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Descendants(System::Xml::Linq::XName ^ name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants(System.Xml.Linq.XName name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Descendants(System.Xml.Linq.XName? name);
member this.Descendants : System.Xml.Linq.XName -> seq<System.Xml.Linq.XElement>
Public Function Descendants (name As XName) As IEnumerable(Of XElement)

Parametrar

name
XName

Att XName matcha.

Returer

En IEnumerable<T> av XElement som innehåller de underordnade elementen i XContainer som matchar den angivna XName.

Exempel

I följande exempel skrivs alla underordnade element ut.

// Attributes are not nodes, so will not be returned by DescendantNodes.
XElement xmlTree = new XElement("Root",
    new XAttribute("Att1", "AttributeContent"),
    new XElement("Child",
        new XText("Some text"),
        new XElement("GrandChild", "element content")
    )
);
IEnumerable<XElement> de =
    from el in xmlTree.Descendants("Child")
    select el;
foreach (XElement el in de)
    Console.WriteLine(el.Name);
' Attributes are not nodes, so will not be returned by the descendants axis.
Dim xmlTree As XElement = _
    <Root Att1="AttributeContent">
         <Child>Some text
             <GrandChild>element content</GrandChild>
         </Child>
     </Root>

Dim de = From el In xmlTree...<Child> _
         Select el

For Each el In de
    Console.WriteLine(el.Name)
Next

Det här exemplet genererar följande utdata:

Child

Följande är samma exempel, men i det här fallet finns XML i ett namnområde. Mer information finns i Arbeta med XML-namnområden.

// Attributes are not nodes, so will not be returned by DescendantNodes.
XNamespace aw = "http://www.adventure-works.com";
XElement xmlTree = new XElement(aw + "Root",
    new XAttribute(aw + "Att1", "AttributeContent"),
    new XElement(aw + "Child",
        new XText("Some text"),
        new XElement(aw + "GrandChild", "element content")
    )
);
IEnumerable<XElement> de =
    from el in xmlTree.Descendants(aw + "Child")
    select el;
foreach (XElement el in de)
    Console.WriteLine(el.Name);
Imports <xmlns:aw = "http://www.adventure-works.com">

Module Module1
    Sub Main()
        ' Attributes are not nodes, so will not be returned by the descendants axis.
        Dim xmlTree As XElement = _
            <aw:Root aw:Att1="AttributeContent">
                 <aw:Child>Some text
                     <aw:GrandChild>element content</aw:GrandChild>
                 </aw:Child>
             </aw:Root>

        Dim de = From el In xmlTree...<aw:Child> _
                 Select el

        For Each el In de
            Console.WriteLine(el.Name)
        Next
    End Sub
End Module

Det här exemplet genererar följande utdata:

{http://www.adventure-works.com}Child

Kommentarer

Den här metoden använder uppskjuten körning.

Se även

Gäller för