次の方法で共有


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> の子孫ノードをドキュメント順に格納している XNodeXContainer

次の例では、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 to XML内のノードとは見なされないため、このメソッドによって返されるコレクションの一部ではないことに注意してください。

このメソッドは遅延実行を使用します。

適用対象

こちらもご覧ください