語言

XContainer.DescendantNodes 方法

定義

回傳該文件或元素的後裔節點集合,依文件順序排列。

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

傳回

IEnumerable<T> XNode包含 的後代節點XContainer,依文件順序排列。

範例

以下範例建立一棵 XML 樹,然後沿軸 DescendantNodes 逐步迭代。

XElement xmlTree = new XElement("Root",
    // Attributes are not nodes, so will not be returned by DescendantNodes.
    new XAttribute("Att1", "AttributeContent"),
    new XElement("Child",
        new XElement("GrandChild", "element content")
    )
);
IEnumerable<XNode> dnas =
    from node in xmlTree.DescendantNodes()
    select node;
foreach (XNode node in dnas)
{
    if (node is XElement)
        Console.WriteLine((node as XElement).Name);
    else
        Console.WriteLine(node);
}
' Attributes are not nodes, so will not be returned by DescendantNodes.
Dim xmlTree As XElement = _
    <Root Att1="AttributeContent">
        <Child>
            <GrandChild>element content</GrandChild>
        </Child>
    </Root>

Dim dnas = From node In xmlTree.DescendantNodes _
           Select node

For Each node In dnas
    If TypeOf node Is XElement Then
        Console.WriteLine(DirectCast(node, XElement).Name)
    Else
        Console.WriteLine(node)
    End If
Next

此範例會產生下列輸出:

Child
GrandChild
element content

備註

請注意,屬性在 LINQ 到 XML 中不被視為節點,因此不會包含在此方法回傳的集合中。

此方法使用延遲執行。

適用於

另請參閱