문서를 XDocument.Load를 통해 로드할 때 작성하는 쿼리는 XElement.Load를 통해 로드할 때 작성하는 쿼리와 약간 다릅니다.
XDocument.Load
및 XElement.Load
비교
XML 문서를 XElement를 통해 XElement.Load에 로드하면, XML 트리의 루트에 있는 XElement에 로드된 문서의 루트 원소가 포함됩니다. 그러나 동일한 XML 문서를 XDocument에 XDocument.Load를 통해 로드할 때, 트리의 루트는 XDocument 노드이고, 로드된 문서의 루트 요소는 XElement의 유일하게 허용된 자식 XDocument 노드입니다. LINQ to XML 축은 루트 노드를 기준으로 작동합니다.
예: XML 트리를 XElement.Load
을/를 사용하여 로드한 후, 자식 요소를 쿼리합니다.
이 첫 번째 예제에서는 .를 사용하여 LoadXML 트리를 로드합니다. 그런 다음 트리 루트의 자식 요소를 쿼리합니다.
// 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
이 예제는 다음과 같은 출력을 생성합니다.
Querying tree loaded with XElement.Load
----
<Child1>1</Child1>
<Child2>2</Child2>
<Child3>3</Child3>
예: XML 트리를 XDocument.Load
을/를 사용하여 로드한 후, 자식 요소를 쿼리합니다.
다음 예제는 위의 예제와 동일한 동작을 수행하지만, XML 트리는 XDocument이 아니라 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
이 예제는 다음과 같은 출력을 생성합니다.
Querying tree loaded with XDocument.Load
----
<Root>
<Child1>1</Child1>
<Child2>2</Child2>
<Child3>3</Child3>
</Root>
동일한 쿼리가 3개의 자식 노드 대신 하나의 Root
노드를 반환했습니다.
이 문제를 처리하는 한 가지 방법은 다음과 같이 축 메서드에 Root 액세스하기 전에 속성을 사용하는 것입니다.
// 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
이제 이 쿼리는 XElement 루트를 가진 트리의 쿼리와 동일한 방법으로 수행됩니다.
이 예제는 다음과 같은 출력을 생성합니다.
Querying tree loaded with XDocument.Load
----
<Child1>1</Child1>
<Child2>2</Child2>
<Child3>3</Child3>
.NET