XDocument.Parse 메서드

정의

문자열에서 새 XDocument 항목을 만들고, 필요에 따라 공백을 유지하고, 기본 URI를 설정하고, 줄 정보를 유지합니다.

오버로드

Name Description
Parse(String)

문자열에서 새 XDocument 문자열을 만듭니다.

Parse(String, LoadOptions)

문자열에서 새 XDocument 항목을 만들고, 필요에 따라 공백을 유지하고, 기본 URI를 설정하고, 줄 정보를 유지합니다.

예제

다음 예제에서는 XML을 포함하는 문자열을 만듭니다. 그런 다음 문자열을 .로 XDocument구문 분석합니다.

string str =
@"<?xml version=""1.0""?>
<!-- comment at the root level -->
<Root>
    <Child>Content</Child>
</Root>";
XDocument doc = XDocument.Parse(str);
Console.WriteLine(doc);
Dim str As String = _
    "<?xml version= '1.0'?>" & _
    "<!-- comment at the root level -->" & _
    "<Root>" & _
    "  <Child>Content</Child>" & _
    "</Root>"

Dim doc As XDocument = XDocument.Parse(str)
Console.WriteLine(doc)

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

<!-- comment at the root level -->
<Root>
  <Child>Content</Child>
</Root>

설명

이 메서드는 문자열을 구문 분석하고 XML 트리를 만듭니다.

Parse(String)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

문자열에서 새 XDocument 문자열을 만듭니다.

public:
 static System::Xml::Linq::XDocument ^ Parse(System::String ^ text);
public static System.Xml.Linq.XDocument Parse(string text);
static member Parse : string -> System.Xml.Linq.XDocument
Public Shared Function Parse (text As String) As XDocument

매개 변수

text
String

XML을 포함하는 문자열입니다.

반품

XDocument XML을 포함하는 문자열에서 채워진 값입니다.

예제

다음 예제에서는 XML을 포함하는 문자열을 만듭니다. 그런 다음 문자열을 .로 XDocument구문 분석합니다.

string str =
@"<?xml version=""1.0""?>
<!-- comment at the root level -->
<Root>
    <Child>Content</Child>
</Root>";
XDocument doc = XDocument.Parse(str);
Console.WriteLine(doc);
Dim str As String = _
    "<?xml version= '1.0'?>" & _
    "<!-- comment at the root level -->" & _
    "<Root>" & _
    "  <Child>Content</Child>" & _
    "</Root>"

Dim doc As XDocument = XDocument.Parse(str)
Console.WriteLine(doc)

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

<!-- comment at the root level -->
<Root>
  <Child>Content</Child>
</Root>

설명

이 메서드는 공백을 유지하지 않습니다. XML 트리에서 공백을 유지하려면 매개 변수로 사용하는 Parse 오버로드 LoadOptions 를 사용합니다.

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

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

추가 정보

적용 대상

Parse(String, LoadOptions)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

문자열에서 새 XDocument 항목을 만들고, 필요에 따라 공백을 유지하고, 기본 URI를 설정하고, 줄 정보를 유지합니다.

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

매개 변수

text
String

XML을 포함하는 문자열입니다.

options
LoadOptions

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

반품

XDocument XML을 포함하는 문자열에서 채워진 값입니다.

예제

다음 예제에서는 문자열을 .로 구문 분석합니다 XDocument.

string str =
@"<?xml version=""1.0""?>
<!-- comment at the root level -->
<Root>
    <Child>Content</Child>
</Root>";
XDocument doc1 = XDocument.Parse(str, LoadOptions.PreserveWhitespace);
Console.WriteLine("nodes when preserving whitespace: {0}", doc1.DescendantNodes().Count());
XDocument doc2 = XDocument.Parse(str, LoadOptions.None);
Console.WriteLine("nodes when not preserving whitespace: {0}", doc2.DescendantNodes().Count());
Dim str As String = _
"<?xml version= '1.0'?>" & Environment.NewLine & _
"<!-- comment at the root level -->" & Environment.NewLine & _
"<Root>" & Environment.NewLine & _
"    <Child>Content</Child>"  & Environment.NewLine & _
"</Root>"

Dim doc1 As XDocument = XDocument.Parse(str, LoadOptions.PreserveWhitespace)
Console.WriteLine("nodes when preserving whitespace: {0}", doc1.DescendantNodes().Count())
Dim doc2 As XDocument = XDocument.Parse(str, LoadOptions.None)
Console.WriteLine("nodes when not preserving whitespace: {0}", doc2.DescendantNodes().Count())

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

nodes when preserving whitespace: 8
nodes when not preserving whitespace: 4

설명

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

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

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

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

에서 구문 분석할 때는 설정 SetBaseUriString유효하지 않습니다.

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

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

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

추가 정보

적용 대상