XNode.ReadFrom(XmlReader) 메서드

정의

에서 만듭니 XNodeXmlReader다.

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

XmlReaderXNode항목을 읽을 노드에 배치된 위치입니다.

반품

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 문서의 스트리밍 변환을 수행하는 방법을 참조하세요.

적용 대상

추가 정보