다음을 통해 공유


XDocument 쿼리 및 XElement 쿼리(LINQ to XML)

XDocument.Load를 통해 문서를 로드할 때 작성하는 쿼리는 XElement.Load를 통해 로드할 때 작성하는 쿼리와 약간 다릅니다.

XDocument.LoadXElement.Load의 비교

XElement를 통해 XML 문서를 XElement.Load에 로드하면 XML 트리의 루트에 있는 XElement에 로드된 문서의 루트 요소가 포함되어 있습니다. 그러나 XDocument를 통해 동일한 XML 문서를 XDocument.Load에 로드하면 트리의 루트가 XDocument 노드이고 로드된 문서의 루트 요소가 XElement의 허용된 하나의 자식 XDocument 노드입니다. LINQ to XML 축은 루트 노드를 기준으로 작동합니다.

예: XElement.Load를 사용하여 XML 트리를 로드한 후 자식 요소에 대해 쿼리

이 첫 번째 예제에서는 Load를 사용하여 XML 트리를 로드한 다음 트리 루트의 자식 요소에 대해 쿼리합니다.

// 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>

예: XDocument.Load를 사용하여 XML 트리를 로드한 후 자식 요소에 대해 쿼리

다음 예는 위의 예와 동일하지만 XML 트리가 XElement가 아닌 XDocument에 로드되었습니다.

// 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>

동일한 쿼리에서 세 자식 노드 대신 하나의 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>