Nota:
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
Cuando carga un documento mediante XDocument.Load, observará que es necesario escribir las consultas de forma ligeramente diferente a como lo haría en caso de cargar el documento mediante XElement.Load.
Comparación entre XDocument.Load y XElement.Load
Cuando carga un documento XML en un XElement mediante XElement.Load, el XElement situado en la raíz del árbol XML contiene al elemento raíz del documento que se ha cargado.Sin embargo, cuando carga el mismo documento XML en un XDocument mediante XDocument.Load, la raíz del árbol es un nodo XDocument el elemento raíz del documento cargado es el nodo secundario permitido XElement de XDocument.Los ejes de LINQ to XML funcionan en relación al nodo raíz.
Este primer ejemplo carga un árbol XML utilizando Load.A continuación, consulta los elementos secundarios de la raíz del árbol.
// 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
Como cabe esperar, este ejemplo genera el siguiente resultado:
Querying tree loaded with XElement.Load
----
<Child1>1</Child1>
<Child2>2</Child2>
<Child3>3</Child3>
El siguiente ejemplo es igual al anterior, con la diferencia de que el árbol XML se carga en un XDocument en vez de un 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 ejemplo genera el siguiente resultado:
Querying tree loaded with XDocument.Load
----
<Root>
<Child1>1</Child1>
<Child2>2</Child2>
<Child3>3</Child3>
</Root>
Observe que la misma consulta devolvió el nodo Root en vez de los tres nodos secundarios.
Una forma de afrontar este asunto es utilizar la propiedad Root antes de obtener acceso a los métodos de los ejes, tal y como se indica a continuación:
// 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
Ahora, esta consulta genera el mismo resultado que la consulta del árbol cuya raíz comienza en XElement.El ejemplo genera el siguiente resultado:
Querying tree loaded with XDocument.Load
----
<Child1>1</Child1>
<Child2>2</Child2>
<Child3>3</Child3>