XNode.ReadFrom(XmlReader) 메서드

정의

XNode를 사용하여 XmlReader를 만듭니다.

public:
 static System::Xml::Linq::XNode ^ ReadFrom(System::Xml::XmlReader ^ reader);
public static System.Xml.Linq.XNode ReadFrom (System.Xml.XmlReader reader);
static member ReadFrom : System.Xml.XmlReader -> System.Xml.Linq.XNode
Public Shared Function ReadFrom (reader As XmlReader) As XNode

매개 변수

reader
XmlReader

XmlReader로 읽어올 노드에 배치된 XNode입니다.

반환

XNode

판독기에서 읽은 노드 및 해당 하위 노드가 들어 있는 XNode입니다. 노드의 런타임 형식은 판독기에서 읽은 첫 번째 노드의 형식(NodeType)에 따라 결정됩니다.

예외

XmlReader가 인식 가능한 노드 형식에 배치되지 않은 경우

내부 XmlReader에서 예외를 throw한 경우

예제

이 예제에서는 Source.xml 다음과 같은 XML 파일을 사용합니다.

<?xml version="1.0" encoding="utf-8" ?>
<Root>
  <Child Key="01">
    <GrandChild>aaa</GrandChild>
  </Child>
  <Child Key="02">
    <GrandChild>bbb</GrandChild>
  </Child>
  <Child Key="03">
    <GrandChild>ccc</GrandChild>
  </Child>
</Root>

다음 예제에서는 LINQ 쿼리를 사용하여 사용자 지정 축을 사용한 ReadFrom 다음 쿼리하는 사용자 지정 축 메서드를 만듭니다.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

class Program
{
    static IEnumerable<XElement> StreamRootChildDoc(string uri)
    {
        using (XmlReader reader = XmlReader.Create(uri))
        {
            reader.MoveToContent();
            
            // Parse the file and return each of the nodes.
            while (!reader.EOF)
            {
                if (reader.NodeType == XmlNodeType.Element && reader.Name == "Child")
                {
                    XElement el = XElement.ReadFrom(reader) as XElement;
                    if (el != null)
                        yield return el;
                }
                else
                {
                    reader.Read();
                }
            }
        }
    }

    static void Main(string[] args)
    {
        IEnumerable<string> grandChildData =
            from el in StreamRootChildDoc("Source.xml")
            where (int)el.Attribute("Key") > 1
            select (string)el.Element("GrandChild");

        foreach (string str in grandChildData)
            Console.WriteLine(str);
    }
}
Imports System.Collections.Generic
Imports System.Linq
Imports System.Xml
Imports System.Xml.Linq

Module Program
    Iterator Function StreamRootChildDoc(ByVal uri As String) As IEnumerable(Of XElement)

        Using reader As XmlReader = XmlReader.Create(uri)
            reader.MoveToContent()

            ' Parse the file and return each of the nodes.
            While Not reader.EOF

                If reader.NodeType = XmlNodeType.Element AndAlso reader.Name = "Child" Then
                    Dim el As XElement = TryCast(XElement.ReadFrom(reader), XElement)
                    If el IsNot Nothing Then Yield el
                Else
                    reader.Read()
                End If
            End While
        End Using
    End Function

    Sub Main(args As String())

        Dim grandChildData As IEnumerable(Of String) =
            From el In StreamRootChildDoc("Source.xml")
            Where CInt(el.Attribute("Key")) > 1
            Select CStr(el.Element("GrandChild"))

        For Each str As String In grandChildData
            Console.WriteLine(str)
        Next

    End Sub

End Module

이 예제는 다음과 같은 출력을 생성합니다.

bbb  
ccc  

설명

이 메서드를 사용하여 노드 컬렉션을 반환하는 메서드를 작성하여 판독기에서 노드를 읽을 때 각 노드를 생성할 수 있습니다. 이 메서드를 사용하면 메모리 공간이 매우 작은 임의로 큰 XML 파일을 처리할 수 있습니다.

이 메서드에 전달하는 판독기는 예외를 throw할 수 있습니다. ReadFrom 는 판독기에서 throw한 모든 예외를 catch하지 않습니다. 처리되지 않은 예외는 호출 ReadFrom된 코드까지 버블링됩니다. 특히 코드를 처리 XmlException할 준비가 되어 있어야 합니다.

더 복잡한 문서를 스트리밍하는 방법의 예는 헤더 정보에 대한 액세스 권한으로 XML 조각을 스트리밍하는 방법을 참조하세요.

OrderBy와 같은 특정 표준 쿼리 연산자는 자신의 소스를 반복하고 모든 데이터를 수집하여 정렬한 다음 시퀀스의 첫 번째 항목을 최종적으로 생성합니다. 첫 번째 항목을 생성하기 전에 소스를 구체화하는 쿼리 연산자를 사용하는 경우 작은 메모리 사용 공간이 유지되지 않습니다.

작은 메모리 공간을 유지하면서 LINQ to XML 사용하여 매우 큰 XML 문서를 변환하는 예제는 큰 XML 문서의 스트리밍 변환을 수행하는 방법을 참조하세요.

적용 대상

추가 정보