XContainer.Descendants 方法

定義

依照文件順序,傳回這個項目或文件之子代項目的集合。

多載

Descendants()

依照文件順序,傳回這個項目或文件之子代項目的集合。

Descendants(XName)

依照文件順序,傳回這個文件或項目之已篩選子代項目的集合。 集合中只會包含具有相符之 XName 的項目。

備註

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

Descendants()

Source:
XContainer.cs
Source:
XContainer.cs
Source:
XContainer.cs

依照文件順序,傳回這個項目或文件之子代項目的集合。

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)

傳回

IEnumerable<T>XElement,包含 XContainer 的子代項目。

範例

下列範例會建立 XML 樹狀結構,然後使用這個軸方法來擷取子系。

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  

這個範例會產生下列輸出:

Child  
GrandChild  

備註

請注意,這個方法不會在產生的 IEnumerable<T> 中傳回本身。 查看 DescendantsAndSelf 您是否需要在結果中包含目前的 XElement

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

另請參閱

適用於

Descendants(XName)

Source:
XContainer.cs
Source:
XContainer.cs
Source:
XContainer.cs

依照文件順序,傳回這個文件或項目之已篩選子代項目的集合。 集合中只會包含具有相符之 XName 的項目。

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)

參數

name
XName

要比對的 XName

傳回

IEnumerable<T>XElement,包含符合指定 XContainerXName 的子代項目。

範例

下列範例會列印專案的所有子代。

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

這個範例會產生下列輸出:

Child  

以下是相同的範例,但在此情況下,XML 位於命名空間中。 如需詳細資訊,請參閱 使用 XML 命名空間

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

這個範例會產生下列輸出:

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

備註

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

另請參閱

適用於