XDocument.Parse 메서드

정의

문자열을 사용하여 새 XDocument를 만들고, 선택적으로 공백 및 줄 정보를 유지하고 기본 URI를 설정합니다.

오버로드

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)

문자열을 사용하여 새 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이 들어 있는 문자열을 사용하여 채운 XDocument입니다.

예제

다음 예제에서는 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 트리에서 공백을 유지하려면 매개 변수로 사용하는 LoadOptions 오버로드 Parse 를 사용합니다.

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

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

추가 정보

적용 대상

Parse(String, LoadOptions)

문자열을 사용하여 새 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와 줄 정보의 로드 여부 및 공백 동작을 지정하는 LoadOptions입니다.

반환

XDocument

XML이 들어 있는 문자열을 사용하여 채운 XDocument입니다.

예제

다음 예제에서는 문자열을 .로 구문 분석합니다 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을 들여쓰면 플래그 optionsPreserveWhitespace 설정하면 판독기가 원본 XML의 모든 공백을 읽습니다. 형식 XText 의 노드는 중요하고 중요하지 않은 공백 모두에 대해 만들어집니다.

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

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

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

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

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

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

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

추가 정보

적용 대상