XContainer.DescendantNodes 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳該文件或元素的後裔節點集合,依文件順序排列。
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 中不被視為節點,因此不會包含在此方法回傳的集合中。
此方法使用延遲執行。