XElement.DescendantNodesAndSelf 方法

定义

返回节点的集合,而这些节点包含此元素以及此元素的所有子代节点,并将它们按文档顺序排列。Returns a collection of nodes that contain this element, and all descendant nodes of this element, in document order.

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

返回

一个包含此元素以及此元素的所有子代节点(按文档顺序排列)的 IEnumerable<T>XNodeAn IEnumerable<T> of XNode that contain this element, and all descendant nodes of this element, in document order.

示例

下面的示例创建一个 XML 树,然后使用此树。The following example creates an XML tree, and then uses this .

XElement xmlTree = new XElement("Root",  
    // Attributes are not nodes, so will not be returned by DescendantNodesAndSelf.  
    new XAttribute("Att1", "AttributeContent"),  
    new XElement("Child",  
        new XText("Some text"),  
        new XElement("GrandChild", "element content")  
    )  
);  
IEnumerable<XNode> dnas =  
    from node in xmlTree.DescendantNodesAndSelf()  
    select node;  
foreach (XNode node in dnas)  
{  
    if (node is XElement)  
        Console.WriteLine((node as XElement).Name);  
    else  
        Console.WriteLine(node);  
}  
Dim xmlTree As XElement = _  
    <Root Att1="AttributeContent">  
        <Child>Some text<GrandChild>element content</GrandChild>  
        </Child>  
    </Root>  
  
Dim dnas As IEnumerable(Of XNode) = _  
    From node In xmlTree.DescendantNodesAndSelf() _  
    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  

该示例产生下面的输出:This example produces the following output:

Root  
Child  
Some text  
GrandChild  
element content  

注解

此方法使用延迟执行。This method uses deferred execution.

适用于

另请参阅