Extensions.Nodes<T>(IEnumerable<T>) 方法

定義

傳回來源集合中每個文件和項目的子節點集合。

C#
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XNode> Nodes<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> Nodes<T> (this System.Collections.Generic.IEnumerable<T?> source) where T : System.Xml.Linq.XContainer;

類型參數

T

source 中物件的型別,限制為 XContainer

參數

source
IEnumerable<T>

IEnumerable<T>XNode,其中包含來源集合。

傳回

IEnumerable<XNode>

來源集合中每個文件和項目的子節點之 IEnumerable<T>XNode

範例

下列範例會擷取專案集合中每個節點的所有子節點,其名稱為 Child

C#
XElement xmlTree = XElement.Parse(  
    @"<Root><Child>aaa<GrandChild>Text</GrandChild>bbb</Child>" +  
    @"<Child>ccc<GrandChild>Text</GrandChild>ddd</Child></Root>");  
IEnumerable<XNode> nodes = xmlTree.Elements("Child").Nodes();  

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;  
    }  
}  

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

Text: aaa  
Element: GrandChild  
Text: bbb  
Text: ccc  
Element: GrandChild  
Text: ddd  

備註

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

適用於

產品 版本
.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
.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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

另請參閱