Nota
O acesso a esta página requer autorização. Pode tentar iniciar sessão ou alterar os diretórios.
O acesso a esta página requer autorização. Pode tentar alterar os diretórios.
Consultar um
A consulta que você escreve quando carrega um documento via XDocument.Load difere ligeiramente do que você escreve quando carrega via XElement.Load.
Comparação de XDocument.Load e XElement.Load
Quando você carrega um documento XML em uma XElement via XElement.Load, o XElement na raiz da árvore XML contém o elemento raiz do documento carregado. No entanto, ao carregar o mesmo documento XML em um XDocument via XDocument.Load, a raiz da árvore é um nó XDocument, e o elemento raiz do documento carregado é o nó filho permitido XElement do XDocument. Os eixos LINQ to XML operam em relação ao nó raiz.
Exemplo: Carregue uma árvore XML usando XElement.Load, em seguida, consulte elementos filho
Este primeiro exemplo carrega uma árvore XML usando Load. Em seguida, consulta os elementos filhos da raiz da árvore.
// Create a simple document and write it to a file
File.WriteAllText("Test.xml", @"<Root>
<Child1>1</Child1>
<Child2>2</Child2>
<Child3>3</Child3>
</Root>");
Console.WriteLine("Querying tree loaded with XElement.Load");
Console.WriteLine("----");
XElement doc = XElement.Load("Test.xml");
IEnumerable<XElement> childList =
from el in doc.Elements()
select el;
foreach (XElement e in childList)
Console.WriteLine(e);
' Create a simple document and write it to a file
File.WriteAllText("Test.xml", "<Root>" + Environment.NewLine + _
" <Child1>1</Child1>" + Environment.NewLine + _
" <Child2>2</Child2>" + Environment.NewLine + _
" <Child3>3</Child3>" + Environment.NewLine + _
"</Root>")
Console.WriteLine("Querying tree loaded with XElement.Load")
Console.WriteLine("----")
Dim doc As XElement = XElement.Load("Test.xml")
Dim childList As IEnumerable(Of XElement) = _
From el In doc.Elements() _
Select el
For Each e As XElement In childList
Console.WriteLine(e)
Next
Este exemplo produz a seguinte saída:
Querying tree loaded with XElement.Load
----
<Child1>1</Child1>
<Child2>2</Child2>
<Child3>3</Child3>
Exemplo: Carregue uma árvore XML usando XDocument.Load, em seguida, consulte elementos filho
O próximo exemplo faz o mesmo que o anterior, mas a árvore XML foi carregada em um XDocument, em vez de um XElement.
// Create a simple document and write it to a file
File.WriteAllText("Test.xml", @"<Root>
<Child1>1</Child1>
<Child2>2</Child2>
<Child3>3</Child3>
</Root>");
Console.WriteLine("Querying tree loaded with XDocument.Load");
Console.WriteLine("----");
XDocument doc = XDocument.Load("Test.xml");
IEnumerable<XElement> childList =
from el in doc.Elements()
select el;
foreach (XElement e in childList)
Console.WriteLine(e);
' Create a simple document and write it to a file
File.WriteAllText("Test.xml", "<Root>" + Environment.NewLine + _
" <Child1>1</Child1>" + Environment.NewLine + _
" <Child2>2</Child2>" + Environment.NewLine + _
" <Child3>3</Child3>" + Environment.NewLine + _
"</Root>")
Console.WriteLine("Querying tree loaded with XDocument.Load")
Console.WriteLine("----")
Dim doc As XDocument = XDocument.Load("Test.xml")
Dim childList As IEnumerable(Of XElement) = _
From el In doc.Elements() _
Select el
For Each e As XElement In childList
Console.WriteLine(e)
Next
Este exemplo produz a seguinte saída:
Querying tree loaded with XDocument.Load
----
<Root>
<Child1>1</Child1>
<Child2>2</Child2>
<Child3>3</Child3>
</Root>
Observe que a mesma consultação retornou um Root nó em vez dos três nós filhos.
Uma abordagem para lidar com isso é usar a Root propriedade antes de acessar os métodos de eixos, da seguinte maneira:
// Create a simple document and write it to a file
File.WriteAllText("Test.xml", @"<Root>
<Child1>1</Child1>
<Child2>2</Child2>
<Child3>3</Child3>
</Root>");
Console.WriteLine("Querying tree loaded with XDocument.Load");
Console.WriteLine("----");
XDocument doc = XDocument.Load("Test.xml");
IEnumerable<XElement> childList =
from el in doc.Root.Elements()
select el;
foreach (XElement e in childList)
Console.WriteLine(e);
' Create a simple document and write it to a file
File.WriteAllText("Test.xml", "<Root>" + Environment.NewLine + _
" <Child1>1</Child1>" + Environment.NewLine + _
" <Child2>2</Child2>" + Environment.NewLine + _
" <Child3>3</Child3>" + Environment.NewLine + _
"</Root>")
Console.WriteLine("Querying tree loaded with XDocument.Load")
Console.WriteLine("----")
Dim doc As XDocument = XDocument.Load("Test.xml")
Dim childList As IEnumerable(Of XElement) = _
From el In doc.Root.Elements() _
Select el
For Each e As XElement In childList
Console.WriteLine(e)
Next
Essa consulta agora é executada da mesma maneira que a consulta na árvore enraizada no XElement.
Este exemplo produz a seguinte saída:
Querying tree loaded with XDocument.Load
----
<Child1>1</Child1>
<Child2>2</Child2>
<Child3>3</Child3>