英語で読む

次の方法で共有


Extensions.DescendantNodes<T>(IEnumerable<T>) メソッド

定義

ソース コレクション内のすべてのドキュメントおよび要素の子孫ノードのコレクションを返します。

C#
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XNode> DescendantNodes<T>(this System.Collections.Generic.IEnumerable<T> source) where T : System.Xml.Linq.XContainer;
C#
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XNode> DescendantNodes<T>(this System.Collections.Generic.IEnumerable<T?> source) where T : System.Xml.Linq.XContainer;

型パラメーター

T

XContainer に制限された、source 内のオブジェクトの型。

パラメーター

source
IEnumerable<T>

ソース コレクションが格納されている IEnumerable<T>XContainer

戻り値

ソース コレクション内のすべてのドキュメントおよび要素の子孫ノードの、IEnumerable<T>XNode

次の例では、2 つの要素のコレクションを取得し、ソース コレクション内のすべての要素のすべての子孫ノードのコレクションを取得します。 要素の GrandChild 属性はノードとして表示されないことに注意してください。

C#
XElement xmlTree = XElement.Parse(  
@"<Root>  
    <Child>aaa<GrandChild anAttribute='xyz'>Text</GrandChild>  
        <!--a comment-->  
        <?xml-stylesheet type='text/xsl' href='test.xsl'?>  
    </Child>  
    <Child>ccc<GrandChild>Text</GrandChild>ddd</Child>  
</Root>");  
IEnumerable<XNode> nodes =  
    from node in xmlTree.Elements("Child").DescendantNodes()  
    select node;  

foreach (XNode node in nodes)  
{  
    switch (node.NodeType)  
    {  
        case XmlNodeType.Element:  
            Console.WriteLine("Element: {0}", ((XElement)node).Name);  
            break;  
        case XmlNodeType.Text:  
            Console.WriteLine("Text: {0}", ((XText)node).Value);  
            break;  
        case XmlNodeType.Comment:  
            Console.WriteLine("Comment: {0}", ((XComment)node).Value);  
            break;  
        case XmlNodeType.ProcessingInstruction:  
            Console.WriteLine("PI: {0}", ((XProcessingInstruction)node).Data);  
            break;  
    }  
}  

この例を実行すると、次の出力が生成されます。

Text: aaa  
Element: GrandChild  
Text: Text  
Comment: a comment  
PI: type='text/xsl' href='test.xsl'  
Text: ccc  
Element: GrandChild  
Text: Text  
Text: ddd  

同じ例を次に示しますが、この場合、XML は名前空間にあります。 詳細については、「 XML 名前空間の操作」を参照してください。

C#
XNamespace aw = "http://www.adventure-works.com";  
XElement xmlTree = XElement.Parse(  
@"<Root xmlns='http://www.adventure-works.com'>  
    <Child>aaa<GrandChild anAttribute='xyz'>Text</GrandChild>  
        <!--a comment-->  
        <?xml-stylesheet type='text/xsl' href='test.xsl'?>  
    </Child>  
    <Child>ccc<GrandChild>Text</GrandChild>ddd</Child>  
</Root>");  
IEnumerable<XNode> nodes =  
    from node in xmlTree.Elements(aw + "Child").DescendantNodes()  
    select node;  

foreach (XNode node in nodes)  
{  
    switch (node.NodeType)  
    {  
        case XmlNodeType.Element:  
            Console.WriteLine("Element: {0}", ((XElement)node).Name);  
            break;  
        case XmlNodeType.Text:  
            Console.WriteLine("Text: {0}", ((XText)node).Value);  
            break;  
        case XmlNodeType.Comment:  
            Console.WriteLine("Comment: {0}", ((XComment)node).Value);  
            break;  
        case XmlNodeType.ProcessingInstruction:  
            Console.WriteLine("PI: {0}", ((XProcessingInstruction)node).Data);  
            break;  
    }  
}  

この例を実行すると、次の出力が生成されます。

Text: aaa  
Element: {http://www.adventure-works.com}GrandChild  
Text: Text  
Comment: a comment  
PI: type='text/xsl' href='test.xsl'  
Text: ccc  
Element: {http://www.adventure-works.com}GrandChild  
Text: Text  
Text: ddd  

注釈

この軸拡張メソッドは、 オブジェクトと XElement オブジェクトでXDocument使用されます。 これらの型はどちらも からXContainer派生するため、このメソッドはソース コレクションを含む の XContainer に対IEnumerable<T>して動作します。

Visual Basic には子孫要素用の統合 XML 軸がありますが、子孫ノードの統合軸がないため、Visual Basic ユーザーはこの軸メソッドを明示的に使用する必要があります。

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

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

こちらもご覧ください