XElement.Load 메서드

정의

URI로 지정된 파일, XElementURI 또는 TextReader에서 새 XmlReader 파일을 만듭니다.

오버로드

Name Description
Load(TextReader, LoadOptions)

XElement 선택적으로 공백을 보존하고 줄 정보를 보존하는 위치에서 TextReader로드합니다.

Load(XmlReader, LoadOptions)

XElement XmlReader필요에 따라 공백 유지, 기본 URI 설정 및 줄 정보 유지에서 로드합니다.

Load(String, LoadOptions)

파일에서 로드하고 XElement , 필요에 따라 공백을 유지하고, 기본 URI를 설정하고, 줄 정보를 유지합니다.

Load(Stream, LoadOptions)

지정된 스트림을 사용하고, 필요에 따라 공백을 유지하고, 기본 URI를 설정하고, 줄 정보를 유지하여 새 XElement 인스턴스를 만듭니다.

Load(TextReader)

에서 로드 XElement 합니다 TextReader.

Load(String)

파일에서 로드합니다 XElement .

Load(Stream)

지정된 스트림을 사용하여 새 XElement 인스턴스를 만듭니다.

Load(XmlReader)

에서 로드 XElement 합니다 XmlReader.

설명

이 메서드의 오버로드 중 하나를 사용하여 파일, XElementa 또는 .에서 로드 TextReader 할 수 있습니다XmlReader.

XML이 XElement 포함된 문자열에서 만들려면 .를 사용합니다 Parse.

Load(TextReader, LoadOptions)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

XElement 선택적으로 공백을 보존하고 줄 정보를 보존하는 위치에서 TextReader로드합니다.

public:
 static System::Xml::Linq::XElement ^ Load(System::IO::TextReader ^ textReader, System::Xml::Linq::LoadOptions options);
public static System.Xml.Linq.XElement Load(System.IO.TextReader textReader, System.Xml.Linq.LoadOptions options);
static member Load : System.IO.TextReader * System.Xml.Linq.LoadOptions -> System.Xml.Linq.XElement
Public Shared Function Load (textReader As TextReader, options As LoadOptions) As XElement

매개 변수

textReader
TextReader

TextReader 콘텐츠에 대해 XElement 읽을 A입니다.

options
LoadOptions

공백 동작 및 기본 URI 및 줄 정보를 로드할지 여부를 지정하는 A LoadOptions 입니다.

반품

XElement 지정된 TextReaderXML에서 읽은 XML을 포함하는 형식입니다.

예제

다음 예제에서는 공백을 보존하고 공백을 XElement 보존하지 않는 두 가지 방법으로 로드 StringReader 합니다. 그런 다음 쿼리를 사용하여 결과 XML 트리의 공백 노드 수를 확인합니다.

TextReader sr;
int whiteSpaceNodes;

sr = new StringReader("<Root> <Child> </Child> </Root>");
XElement xmlTree1 = XElement.Load(sr, LoadOptions.None);
sr.Close();
whiteSpaceNodes = xmlTree1
    .DescendantNodesAndSelf()
    .OfType<XText>()
    .Where(tNode => tNode.ToString().Trim().Length == 0)
    .Count();
Console.WriteLine("Count of white space nodes (not preserving whitespace): {0}", whiteSpaceNodes);

sr = new StringReader("<Root> <Child> </Child> </Root>");
XElement xmlTree2 = XElement.Load(sr, LoadOptions.PreserveWhitespace);
sr.Close();
whiteSpaceNodes = xmlTree2
    .DescendantNodesAndSelf()
    .OfType<XText>()
    .Where(tNode => tNode.ToString().Trim().Length == 0)
    .Count();
Console.WriteLine("Count of white space nodes (preserving whitespace): {0}", whiteSpaceNodes);
Dim sr As TextReader
Dim whiteSpaceNodes As Integer

sr = New StringReader("<Root> <Child> </Child> </Root>")
Dim xmlTree1 As XElement = XElement.Load(sr, LoadOptions.None)
sr.Close()
whiteSpaceNodes = xmlTree1 _
    .DescendantNodesAndSelf() _
    .OfType(Of XText)() _
    .Where(Function(ByVal tNode As XNode) tNode.ToString().Trim().Length = 0) _
    .Count()
Console.WriteLine("Count of white space nodes (not preserving whitespace): {0}", whiteSpaceNodes)

sr = New StringReader("<Root> <Child> </Child> </Root>")
Dim xmlTree2 As XElement = XElement.Load(sr, LoadOptions.PreserveWhitespace)
sr.Close()
whiteSpaceNodes = xmlTree2 _
    .DescendantNodesAndSelf() _
    .OfType(Of XText)() _
    .Where(Function(ByVal tNode As XNode) tNode.ToString().Trim().Length = 0) _
    .Count()
Console.WriteLine("Count of white space nodes (preserving whitespace): {0}", whiteSpaceNodes)

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

Count of white space nodes (not preserving whitespace): 0
Count of white space nodes (preserving whitespace): 3

다음 예제에서는 줄 정보를 로드할 때 해당 TextReader줄 정보를 로드합니다. 그런 다음, 줄 정보를 출력합니다.

TextReader sr = new StringReader(
@"<Root>
  <Child>
    <GrandChild1/>
    <GrandChild2/>
  </Child>
</Root>");
XElement po = XElement.Load(sr,
    LoadOptions.SetLineInfo);
Console.WriteLine("{0}{1}{2}",
    "Element Name".PadRight(20),
    "Line".PadRight(5),
    "Position");
Console.WriteLine("{0}{1}{2}",
    "------------".PadRight(20),
    "----".PadRight(5),
    "--------");
foreach (XElement e in po.DescendantsAndSelf())
    Console.WriteLine("{0}{1}{2}",
        ("".PadRight(e.Ancestors().Count() * 2) + e.Name).PadRight(20),
        ((IXmlLineInfo)e).LineNumber.ToString().PadRight(5),
        ((IXmlLineInfo)e).LinePosition);
Dim sr As TextReader = New StringReader( _
    "<Root>" & Environment.NewLine & _
    "  <Child>" & Environment.NewLine & _
    "    <GrandChild1/>" & Environment.NewLine & _
    "    <GrandChild2/>" & Environment.NewLine & _
    "  </Child>" & Environment.NewLine & _
    "</Root>")
Dim po As XElement = XElement.Load(sr, LoadOptions.SetLineInfo)
Console.WriteLine("{0}{1}{2}", _
    "Element Name".PadRight(20), _
    "Line".PadRight(5), _
    "Position")
Console.WriteLine("{0}{1}{2}", _
    "------------".PadRight(20), _
    "----".PadRight(5), _
    "--------")
For Each e As XElement In po.DescendantsAndSelf()
    Console.WriteLine("{0}{1}{2}", _
        ("".PadRight(e.Ancestors().Count() * 2) & e.Name.ToString).PadRight(20), _
        (DirectCast(e, IXmlLineInfo)).LineNumber.ToString().PadRight(5), _
        (DirectCast(e, IXmlLineInfo)).LinePosition)
Next

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

Element Name        Line Position
------------        ---- --------
Root                1    2
  Child             2    4
    GrandChild1     3    6
    GrandChild2     4    6

설명

원본 XML을 들여쓰는 경우 플래그 PreserveWhitespaceoptions 설정하면 판독기가 원본 XML의 모든 공백을 읽습니다. 형식 XText 의 노드는 중요하고 중요하지 않은 공백 모두에 대해 생성됩니다.

원본 XML을 들여쓰는 경우 플래그 PreserveWhitespaceoptions 설정하지 않으면 판독기가 원본 XML의 모든 중요하지 않은 공백을 무시하게 됩니다. XML 트리는 중요하지 않은 공백에 대한 텍스트 노드 없이 만들어집니다.

원본 XML을 들여쓰지 않으면 플래그 PreserveWhitespaceoptions 설정해도 효과가 없습니다. 상당한 공백은 여전히 유지되며, 더 많은 공백 텍스트 노드를 만들 수 있는 중요하지 않은 공백의 범위가 없습니다.

자세한 내용은 XML을 로드하거나 구문 분석하는 동안 공백 유지를 참조하고 직렬화하는 동안 공백을 유지합니다.

XML을 Parse 포함하는 문자열에서 만드는 데 사용합니다XElement.

에서 로드SetBaseUri할 때는 설정 TextReader 이 적용되지 않습니다.

플래그를 설정하면 성능 저하가 SetLineInfo 있습니다.

줄 정보는 XML 문서를 로드한 직후에 정확합니다. 문서를 로드한 후 XML 트리를 수정하면 줄 정보가 의미가 없게 될 수 있습니다.

LINQ to XML의 로드 기능은 기본으로 빌드됩니다 XmlReader. 따라서 오버로드 메서드 및 XmlReader.Create 문서를 읽고 구문 분석하는 메서드에 의해 XmlReader throw되는 예외를 catch할 수 있습니다.

추가 정보

적용 대상

Load(XmlReader, LoadOptions)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

XElement XmlReader필요에 따라 공백 유지, 기본 URI 설정 및 줄 정보 유지에서 로드합니다.

public:
 static System::Xml::Linq::XElement ^ Load(System::Xml::XmlReader ^ reader, System::Xml::Linq::LoadOptions options);
public static System.Xml.Linq.XElement Load(System.Xml.XmlReader reader, System.Xml.Linq.LoadOptions options);
static member Load : System.Xml.XmlReader * System.Xml.Linq.LoadOptions -> System.Xml.Linq.XElement
Public Shared Function Load (reader As XmlReader, options As LoadOptions) As XElement

매개 변수

reader
XmlReader

의 내용XmlReader에 대해 읽을 A XElement 입니다.

options
LoadOptions

공백 동작 및 기본 URI 및 줄 정보를 로드할지 여부를 지정하는 A LoadOptions 입니다.

반품

XElement 지정된 XmlReaderXML에서 읽은 XML을 포함하는 형식입니다.

예제

다음 예제에서는 에서 로드하는 줄 정보를 로드합니다 XmlReader. 그런 다음, 줄 정보를 출력합니다.

string markup =
@"<Root>
    <Child>
        <GrandChild/>
    </Child>
</Root>";

// Create a reader and move to the content.
using (XmlReader nodeReader = XmlReader.Create(new StringReader(markup)))
{
    // the reader must be in the Interactive state in order to
    // Create a LINQ to XML tree from it.
    nodeReader.MoveToContent();

    XElement xRoot = XElement.Load(nodeReader, LoadOptions.SetLineInfo);
    Console.WriteLine("{0}{1}{2}",
        "Element Name".PadRight(20),
        "Line".PadRight(5),
        "Position");
    Console.WriteLine("{0}{1}{2}",
        "------------".PadRight(20),
        "----".PadRight(5),
        "--------");
    foreach (XElement e in xRoot.DescendantsAndSelf())
        Console.WriteLine("{0}{1}{2}",
            ("".PadRight(e.Ancestors().Count() * 2) + e.Name).PadRight(20),
            ((IXmlLineInfo)e).LineNumber.ToString().PadRight(5),
            ((IXmlLineInfo)e).LinePosition);
}
Dim markup As String = _
    "<Root>" & Environment.NewLine & _
    "    <Child>" & Environment.NewLine & _
    "        <GrandChild/>" & Environment.NewLine & _
    "    </Child>" & Environment.NewLine & _
    "</Root>"

' Create a reader and move to the content.
Using nodeReader As XmlReader = XmlReader.Create(New StringReader(markup))

    ' the reader must be in the Interactive state in order to
    ' Create a LINQ to XML tree from it.
    nodeReader.MoveToContent()

    Dim xRoot As XElement = XElement.Load(nodeReader, LoadOptions.SetLineInfo)
    Console.WriteLine("{0}{1}{2}", _
        "Element Name".PadRight(20), _
        "Line".PadRight(5), _
        "Position")
    Console.WriteLine("{0}{1}{2}", _
        "------------".PadRight(20), _
        "----".PadRight(5), _
        "--------")
    For Each e As XElement In xRoot.DescendantsAndSelf()
        Console.WriteLine("{0}{1}{2}", _
            ("".PadRight(e.Ancestors().Count() * 2) & e.Name.ToString).PadRight(20), _
            (DirectCast(e, IXmlLineInfo)).LineNumber.ToString().PadRight(5), _
            (DirectCast(e, IXmlLineInfo)).LinePosition)
    Next
End Using

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

Element Name        Line Position
------------        ---- --------
Root                1    2
  Child             2    6
    GrandChild      3    10

설명

DOM 문서에서 만든 XmlNodeReader 다음 이 메서드를 XmlNodeReader사용하여 XElement LINQ to XML 트리에서 DOM 문서의 복사본을 만들 수 있습니다.

XML을 Parse 포함하는 문자열에서 만드는 데 사용합니다XElement.

에서 로드할 때 설정 PreserveWhitespaceXmlReader유효하지 않습니다. XmlReader 공백을 읽거나 읽지 않도록 구성됩니다. LINQ to XML 트리는 판독기에서 표시하는 공백 노드로 채워집니다. 설정 여부에 PreserveWhitespace 관계없이 동작이 됩니다.

XmlReader 유효한 기본 URI가 있을 수 있습니다. 설정하는 SetBaseUri경우 기본 URI는 XML 트리에서 보고 XmlReader되는 기본 URI에서 설정됩니다.

XmlReader 유효한 줄 정보가 있을 수 있습니다. 설정하는 SetLineInfo경우 줄 정보는 XML 트리에서 보고 XmlReader되는 줄 정보에서 설정됩니다.

플래그를 설정하면 성능 저하가 SetLineInfo 있습니다.

줄 정보는 XML 문서를 로드한 직후에 정확합니다. 문서를 로드한 후 XML 트리를 수정하면 줄 정보가 의미가 없게 될 수 있습니다.

LINQ to XML의 로드 기능은 기본으로 빌드됩니다 XmlReader. 따라서 오버로드 메서드 및 XmlReader.Create 문서를 읽고 구문 분석하는 메서드에 의해 XmlReader throw되는 예외를 catch할 수 있습니다.

추가 정보

적용 대상

Load(String, LoadOptions)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

파일에서 로드하고 XElement , 필요에 따라 공백을 유지하고, 기본 URI를 설정하고, 줄 정보를 유지합니다.

public:
 static System::Xml::Linq::XElement ^ Load(System::String ^ uri, System::Xml::Linq::LoadOptions options);
public static System.Xml.Linq.XElement Load(string uri, System.Xml.Linq.LoadOptions options);
static member Load : string * System.Xml.Linq.LoadOptions -> System.Xml.Linq.XElement
Public Shared Function Load (uri As String, options As LoadOptions) As XElement

매개 변수

uri
String

에 로드할 파일을 참조하는 URI 문자열입니다 XElement.

options
LoadOptions

공백 동작 및 기본 URI 및 줄 정보를 로드할지 여부를 지정하는 A LoadOptions 입니다.

반품

XElement 지정된 파일의 내용이 들어 있는 항목입니다.

예제

다음 예제에서는 공백을 보존하고 공백을 유지하지 않는 두 가지 방법으로 파일에서 로드 XElement 합니다. 그런 다음 쿼리를 사용하여 결과 XML 트리의 공백 노드 수를 확인합니다.

XElement xmlTree1 = XElement.Parse("<Root> <Child>  </Child> </Root>", LoadOptions.PreserveWhitespace);
xmlTree1.Save("Tree.xml");
Console.WriteLine(xmlTree1);

int whiteSpaceNodes;
XElement xmlTree2 = XElement.Load("Tree.xml",
    LoadOptions.None);
whiteSpaceNodes = xmlTree2
    .DescendantNodesAndSelf()
    .OfType<XText>()
    .Where(tNode => tNode.ToString().Trim().Length == 0)
    .Count();
Console.WriteLine("Count of white space nodes (not preserving whitespace): {0}", whiteSpaceNodes);

XElement xmlTree3 = XElement.Load("Tree.xml",
    LoadOptions.PreserveWhitespace);
whiteSpaceNodes = xmlTree3
    .DescendantNodesAndSelf()
    .OfType<XText>()
    .Where(tNode => tNode.ToString().Trim().Length == 0)
    .Count();
Console.WriteLine("Count of white space nodes (preserving whitespace): {0}", whiteSpaceNodes);
Dim xmlTree1 As XElement = XElement.Parse("<Root> <Child>  </Child> </Root>", LoadOptions.PreserveWhitespace)
xmlTree1.Save("Tree.xml")
Console.WriteLine(xmlTree1)

Dim whiteSpaceNodes As Integer
Dim xmlTree2 As XElement = XElement.Load("Tree.xml", LoadOptions.None)
whiteSpaceNodes = xmlTree2 _
                  .DescendantNodesAndSelf() _
                  .OfType(Of XText)() _
                  .Where(Function(ByVal tNode As XNode) tNode.ToString().Trim().Length = 0) _
                  .Count()
Console.WriteLine("Count of white space nodes (not preserving whitespace): {0}", whiteSpaceNodes)

Dim xmlTree3 As XElement = XElement.Load("Tree.xml", LoadOptions.PreserveWhitespace)
whiteSpaceNodes = xmlTree3 _
                  .DescendantNodesAndSelf() _
                  .OfType(Of XText)() _
                  .Where(Function(ByVal tNode As XNode) tNode.ToString().Trim().Length = 0) _
                  .Count()
Console.WriteLine("Count of white space nodes (preserving whitespace): {0}", whiteSpaceNodes)

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

<Root> <Child>  </Child> </Root>
Count of white space nodes (not preserving whitespace): 0
Count of white space nodes (preserving whitespace): 3

다음 예제에서는 파일을 로드할 때 기본 URI 및 줄 정보를 로드합니다. 그런 다음 기본 URI 및 줄 정보를 출력합니다.

이 예제에서는 다음 리소스 파일을 사용합니다. 샘플 XML 파일: 일반적인 구매 주문(LINQ to XML).

XElement po = XElement.Load("PurchaseOrder.xml",
    LoadOptions.SetBaseUri | LoadOptions.SetLineInfo);
string[] splitUri = po.BaseUri.Split('/');
Console.WriteLine("BaseUri: {0}", splitUri[splitUri.Length - 1]);
Console.WriteLine();
Console.WriteLine("{0}{1}{2}",
    "Element Name".PadRight(20),
    "Line".PadRight(5),
    "Position");
Console.WriteLine("{0}{1}{2}",
    "------------".PadRight(20),
    "----".PadRight(5),
    "--------");
foreach (XElement e in po.DescendantsAndSelf())
    Console.WriteLine("{0}{1}{2}",
        ("".PadRight(e.Ancestors().Count() * 2) + e.Name).PadRight(20),
        ((IXmlLineInfo)e).LineNumber.ToString().PadRight(5),
        ((IXmlLineInfo)e).LinePosition);
Dim po As XElement = XElement.Load("PurchaseOrder.xml", LoadOptions.SetBaseUri Or LoadOptions.SetLineInfo)
Dim splitUri() As String = po.BaseUri.Split("/")
Console.WriteLine("BaseUri: {0}", splitUri(splitUri.Length - 1))
Console.WriteLine()
Console.WriteLine("{0}{1}{2}", _
    "Element Name".PadRight(20), _
    "Line".PadRight(5), _
    "Position")
Console.WriteLine("{0}{1}{2}", _
    "------------".PadRight(20), _
    "----".PadRight(5), _
    "--------")
For Each e As XElement In po.DescendantsAndSelf()
    Console.WriteLine("{0}{1}{2}", _
        ("".PadRight(e.Ancestors().Count() * 2) & e.Name.ToString()).PadRight(20), _
        (DirectCast(e, IXmlLineInfo)).LineNumber.ToString().PadRight(5), _
        (DirectCast(e, IXmlLineInfo)).LinePosition)
Next

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

BaseUri: PurchaseOrder.xml

Element Name        Line Position
------------        ---- --------
PurchaseOrder       2    2
  Address           3    4
    Name            4    6
    Street          5    6
    City            6    6
    State           7    6
    Zip             8    6
    Country         9    6
  Address           11   4
    Name            12   6
    Street          13   6
    City            14   6
    State           15   6
    Zip             16   6
    Country         17   6
  DeliveryNotes     19   4
  Items             20   4
    Item            21   6
      ProductName   22   8
      Quantity      23   8
      USPrice       24   8
      Comment       25   8
    Item            27   6
      ProductName   28   8
      Quantity      29   8
      USPrice       30   8
      ShipDate      31   8

설명

원본 XML을 들여쓰는 경우 플래그 PreserveWhitespaceoptions 설정하면 판독기가 원본 XML의 모든 공백을 읽습니다. 형식 XText 의 노드는 중요하고 중요하지 않은 공백 모두에 대해 생성됩니다.

원본 XML을 들여쓰는 경우 플래그 PreserveWhitespaceoptions 설정하지 않으면 판독기가 원본 XML의 모든 중요하지 않은 공백을 무시하게 됩니다. XML 트리는 중요하지 않은 공백에 대한 텍스트 노드 없이 만들어집니다.

원본 XML을 들여쓰지 않으면 플래그 PreserveWhitespaceoptions 설정해도 효과가 없습니다. 상당한 공백은 여전히 유지되며, 더 많은 공백 텍스트 노드를 만들 수 있는 중요하지 않은 공백의 범위가 없습니다.

자세한 내용은 XML을 로드하거나 구문 분석하는 동안 공백 유지를 참조하고 직렬화하는 동안 공백을 유지합니다.

XML을 Parse 포함하는 문자열에서 만드는 데 사용합니다XElement.

플래그와 플래그를 설정 SetBaseUri 하면 성능이 저하됩니다 SetLineInfo .

XML 문서를 로드한 직후 기본 URI 및 줄 정보가 정확합니다. 문서를 로드한 후 XML 트리를 수정하면 기본 URI 및 줄 정보가 의미가 없게 될 수 있습니다.

LINQ to XML의 로드 기능은 기본으로 빌드됩니다 XmlReader. 따라서 오버로드 메서드 및 XmlReader.Create 문서를 읽고 구문 분석하는 메서드에 의해 XmlReader throw되는 예외를 catch할 수 있습니다.

추가 정보

적용 대상

Load(Stream, LoadOptions)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

지정된 스트림을 사용하고, 필요에 따라 공백을 유지하고, 기본 URI를 설정하고, 줄 정보를 유지하여 새 XElement 인스턴스를 만듭니다.

public:
 static System::Xml::Linq::XElement ^ Load(System::IO::Stream ^ stream, System::Xml::Linq::LoadOptions options);
public static System.Xml.Linq.XElement Load(System.IO.Stream stream, System.Xml.Linq.LoadOptions options);
static member Load : System.IO.Stream * System.Xml.Linq.LoadOptions -> System.Xml.Linq.XElement
Public Shared Function Load (stream As Stream, options As LoadOptions) As XElement

매개 변수

stream
Stream

XML 데이터를 포함하는 스트림입니다.

options
LoadOptions

LoadOptions 기본 URI 및 줄 정보를 로드할지 여부를 지정하는 개체입니다.

반품

XElement 스트림에 포함된 데이터를 읽는 데 사용되는 개체입니다.

설명

LINQ to XML의 로드 기능은 기본으로 빌드됩니다 XmlReader. 따라서 오버로드 메서드 및 XmlReader.Create 문서를 읽고 구문 분석하는 메서드에 의해 XmlReader throw되는 예외를 catch할 수 있습니다.

수정 XmlReaderSettings해야 하는 경우 다음 단계를 수행합니다.

  1. XmlReader 매개 변수로 사용하는 Create 오버로드 중 XmlReaderSettings 하나를 호출하여 만듭니다.

  2. XmlReader 매개 변수로 사용하는 XElement '의 Load 오버로드 중 하나에 XmlReader전달합니다.

적용 대상

Load(TextReader)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

에서 로드 XElement 합니다 TextReader.

public:
 static System::Xml::Linq::XElement ^ Load(System::IO::TextReader ^ textReader);
public static System.Xml.Linq.XElement Load(System.IO.TextReader textReader);
static member Load : System.IO.TextReader -> System.Xml.Linq.XElement
Public Shared Function Load (textReader As TextReader) As XElement

매개 변수

textReader
TextReader

TextReader 콘텐츠에 대해 XElement 읽을 A입니다.

반품

XElement 지정된 TextReaderXML에서 읽은 XML을 포함하는 형식입니다.

예제

다음 예제에서는 .에서 요소를 로드합니다 StringReader.

TextReader sr = new StringReader("<Root><Child/></Root>");
XElement xmlTree = XElement.Load(sr);
sr.Close();
Console.WriteLine(xmlTree);
Dim sr As TextReader = New StringReader("<Root><Child/></Root>")
Dim xmlTree As XElement = XElement.Load(sr)
sr.Close()
Console.WriteLine(xmlTree)

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

<Root>
  <Child />
</Root>

설명

이 메서드는 원시 XML을 XML 트리로 읽습니다. 파일의 모든 중요하지 않은 공백을 삭제합니다.

LINQ to XML의 로드 기능은 기본으로 빌드됩니다 XmlReader. 따라서 오버로드 메서드 및 XmlReader.Create 문서를 읽고 구문 분석하는 메서드에 의해 XmlReader throw되는 예외를 catch할 수 있습니다.

추가 정보

적용 대상

Load(String)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

파일에서 로드합니다 XElement .

public:
 static System::Xml::Linq::XElement ^ Load(System::String ^ uri);
public static System.Xml.Linq.XElement Load(string uri);
static member Load : string -> System.Xml.Linq.XElement
Public Shared Function Load (uri As String) As XElement

매개 변수

uri
String

XElement파일로 로드할 파일을 참조하는 URI 문자열입니다.

반품

XElement 지정된 파일의 내용이 들어 있는 항목입니다.

예제

다음 예제에서는 XML 트리를 만들고 파일에 저장한 다음 이 메서드를 사용하여 파일에서 로드합니다 XElement .

XElement xmlTree1 = new XElement("Root",
    new XElement("Child", "content")
);
xmlTree1.Save("Tree.xml");

XElement xmlTree2 = XElement.Load("Tree.xml");
Console.WriteLine(xmlTree2.Name);
Dim xmlTree1 As XElement = _
        <Root>
            <Child>Content</Child>
        </Root>
xmlTree1.Save("Tree.xml")

Dim xmlTree2 As XElement = XElement.Load("Tree.xml")
Console.WriteLine(xmlTree2.Name)

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

Root

설명

이 메서드는 원시 XML을 XML 트리로 읽습니다. 파일의 모든 중요하지 않은 공백을 삭제합니다.

LINQ to XML의 로드 기능은 기본으로 빌드됩니다 XmlReader. 따라서 오버로드 메서드 및 XmlReader.Create 문서를 읽고 구문 분석하는 메서드에 의해 XmlReader throw되는 예외를 catch할 수 있습니다.

추가 정보

적용 대상

Load(Stream)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

지정된 스트림을 사용하여 새 XElement 인스턴스를 만듭니다.

public:
 static System::Xml::Linq::XElement ^ Load(System::IO::Stream ^ stream);
public static System.Xml.Linq.XElement Load(System.IO.Stream stream);
static member Load : System.IO.Stream -> System.Xml.Linq.XElement
Public Shared Function Load (stream As Stream) As XElement

매개 변수

stream
Stream

XML 데이터를 포함하는 스트림입니다.

반품

XElement 스트림에 포함된 데이터를 읽는 데 사용되는 개체입니다.

설명

로드 옵션을 제어하려면 매개 변수로 사용하는 Load 오버로드를 사용합니다LoadOptions.

LINQ to XML의 로드 기능은 기본으로 빌드됩니다 XmlReader. 따라서 오버로드 메서드 및 XmlReader.Create 문서를 읽고 구문 분석하는 메서드에 의해 XmlReader throw되는 예외를 catch할 수 있습니다.

수정 XmlReaderSettings해야 하는 경우 다음 단계를 수행합니다.

  1. XmlReader 매개 변수로 사용하는 Create 오버로드 중 XmlReaderSettings 하나를 호출하여 만듭니다.

  2. XmlReader 매개 변수로 사용하는 XElement '의 Load 오버로드 중 하나에 XmlReader전달합니다.

적용 대상

Load(XmlReader)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

에서 로드 XElement 합니다 XmlReader.

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

매개 변수

reader
XmlReader

의 내용XmlReader에 대해 읽을 A XElement 입니다.

반품

XElement 지정된 XmlReaderXML에서 읽은 XML을 포함하는 형식입니다.

예제

다음 예제에서는 DOM 문서를 만들고, DOM 문서에서 만들고 XmlNodeReader , 판독기에서 트리를 인스턴스화합니다. 이 코드는 DOM 문서를 LINQ to XML 트리에 효과적으로 복사합니다.

// Create a DOM document with some content.
XmlDocument doc = new XmlDocument();
XmlElement child = doc.CreateElement("Child");
child.InnerText = "child contents";
XmlElement root = doc.CreateElement("Root");
root.AppendChild(child);
doc.AppendChild(root);

// Create a reader and move to the content.
using (XmlNodeReader nodeReader = new XmlNodeReader(doc)) {
    // the reader must be in the Interactive state in order to
    // Create a LINQ to XML tree from it.
    nodeReader.MoveToContent();

    XElement xRoot = XElement.Load(nodeReader);
    Console.WriteLine(xRoot);
}
' Create a DOM document with some content.
Dim doc As XmlDocument = New XmlDocument()
Dim child As XmlElement = doc.CreateElement("Child")
child.InnerText = "child contents"
Dim root As XmlElement = doc.CreateElement("Root")
root.AppendChild(child)
doc.AppendChild(root)

' Create a reader and move to the content.
Using nodeReader = New XmlNodeReader(doc)
    ' the reader must be in the Interactive state in order to
    ' Create a LINQ to XML tree from it.
    nodeReader.MoveToContent()

    Dim xRoot As XElement = XElement.Load(nodeReader)
    Console.WriteLine(xRoot)
End Using

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

<Root>
  <Child>child contents</Child>
</Root>

설명

DOM 문서에서 만든 XmlNodeReader 다음 이 메서드를 XmlNodeReader사용하여 XElement LINQ to XML 트리에서 DOM 문서의 복사본을 만들 수 있습니다.

LINQ to XML의 로드 기능은 기본으로 빌드됩니다 XmlReader. 따라서 오버로드 메서드 및 XmlReader.Create 문서를 읽고 구문 분석하는 메서드에 의해 XmlReader throw되는 예외를 catch할 수 있습니다.

추가 정보

적용 대상