Extensions.DescendantNodes<T>(IEnumerable<T>) Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Devolve uma coleção dos nós descendentes de cada documento e elemento da coleção de origem.
public:
generic <typename T>
where T : System::Xml::Linq::XContainer[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<System::Xml::Linq::XNode ^> ^ DescendantNodes(System::Collections::Generic::IEnumerable<T> ^ source);
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;
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;
static member DescendantNodes : seq<'T (requires 'T :> System.Xml.Linq.XContainer)> -> seq<System.Xml.Linq.XNode> (requires 'T :> System.Xml.Linq.XContainer)
<Extension()>
Public Function DescendantNodes(Of T As XContainer) (source As IEnumerable(Of T)) As IEnumerable(Of XNode)
Parâmetros de Tipo Genérico
- T
O tipo dos objetos em source, restrito a XContainer.
Parâmetros
- source
- IEnumerable<T>
Um IEnumerable<T> deles XContainer contém a coleção de fontes.
Devoluções
Um IEnumerable<T> dos XNode nós descendentes de cada documento e elemento na coleção fonte.
Exemplos
O exemplo seguinte recupera uma coleção de dois elementos e depois recupera uma coleção de todos os nós descendentes para cada elemento da coleção fonte. Note que o atributo do GrandChild elemento não é apresentado como nó.
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;
}
}
Dim xmlTree As XElement = _
<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>
Dim nodes As IEnumerable(Of XNode) = _
From node In xmlTree.<Child>.DescendantNodes _
Select node
For Each node As XNode In nodes
Select Case node.NodeType
Case XmlNodeType.Element
Console.WriteLine("Element: {0}", DirectCast(node, XElement).Name)
Case XmlNodeType.Text
Console.WriteLine("Text: {0}", DirectCast(node, XText).Value)
Case XmlNodeType.Comment
Console.WriteLine("Comment: {0}", DirectCast(node, XComment).Value)
Case XmlNodeType.ProcessingInstruction
Console.WriteLine("PI: {0}", DirectCast(node, XProcessingInstruction).Data)
End Select
Next
Este exemplo produz a seguinte saída:
Text: aaa
Element: GrandChild
Text: Text
Comment: a comment
PI: type='text/xsl' href='test.xsl'
Text: ccc
Element: GrandChild
Text: Text
Text: ddd
O exemplo seguinte é o mesmo, mas neste caso o XML está num namespace. Para mais informações, consulte Trabalhar com Namespaces XML.
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;
}
}
Imports <xmlns="http://www.adventure-works.com">
Module Module1
Sub Main()
Dim xmlTree As XElement = _
<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>
Dim nodes As IEnumerable(Of XNode) = _
From node In xmlTree.<Child>.DescendantNodes _
Select node
For Each node As XNode In nodes
Select Case node.NodeType
Case XmlNodeType.Element
Console.WriteLine("Element: {0}", DirectCast(node, XElement).Name)
Case XmlNodeType.Text
Console.WriteLine("Text: {0}", DirectCast(node, XText).Value)
Case XmlNodeType.Comment
Console.WriteLine("Comment: {0}", DirectCast(node, XComment).Value)
Case XmlNodeType.ProcessingInstruction
Console.WriteLine("PI: {0}", DirectCast(node, XProcessingInstruction).Data)
End Select
Next
End Sub
End Module
Este exemplo produz a seguinte saída:
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
Observações
Este método de extensão de eixo é usado em XDocument e XElement objetos. Ambos estes tipos derivam de XContainer, pelo que este método opera sobre um IEnumerable<T> de XContainer que contém a coleção fonte.
Embora o Visual Basic tenha um eixo XML integrado para os elementos descendentes, não existe um eixo integrado para nós descendentes, pelo que os utilizadores do Visual Basic devem usar este método de eixo explicitamente.
Este método utiliza execução diferida.