XElement.Parse 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
XElement 필요에 따라 공백을 유지하고 줄 정보를 유지하는 XML이 포함된 문자열에서 로드합니다.
오버로드
| Name | Description |
|---|---|
| Parse(String) |
XML이 XElement 포함된 문자열에서 로드합니다. |
| Parse(String, LoadOptions) |
XElement 필요에 따라 공백을 유지하고 줄 정보를 유지하는 XML이 포함된 문자열에서 로드합니다. |
Parse(String)
- Source:
- XElement.cs
- Source:
- XElement.cs
- Source:
- XElement.cs
- Source:
- XElement.cs
- Source:
- XElement.cs
XML이 XElement 포함된 문자열에서 로드합니다.
public:
static System::Xml::Linq::XElement ^ Parse(System::String ^ text);
public static System.Xml.Linq.XElement Parse(string text);
static member Parse : string -> System.Xml.Linq.XElement
Public Shared Function Parse (text As String) As XElement
매개 변수
반품
XElement XML을 포함하는 문자열에서 채워진 값입니다.
예제
다음 예제에서는 XML을 포함하는 문자열을 만듭니다. 그런 다음 문자열을 .로 XElement구문 분석합니다.
XElement xmlTree = XElement.Parse("<Root> <Child> </Child> </Root>");
Console.WriteLine(xmlTree);
Dim xmlTree As XElement = <Root><Child></Child></Root>
Console.WriteLine(xmlTree)
이 예제는 다음과 같은 출력을 생성합니다.
<Root>
<Child></Child>
</Root>
설명
이 메서드는 공백을 유지하지 않습니다. XML 트리에서 공백을 유지하려면 매개 변수로 사용하는 Parse 메서드의 LoadOptions 오버로드를 사용합니다. 자세한 내용은 XML을 로드하거나 구문 분석하는 동안 공백 유지를 참조하고 직렬화하는 동안 공백을 유지합니다.
LINQ to XML의 로드 기능은 기본으로 빌드됩니다 XmlReader. 따라서 오버로드 메서드 및 XmlReader.Create 문서를 읽고 구문 분석하는 메서드에 의해 XmlReader throw되는 예외를 catch할 수 있습니다.
추가 정보
적용 대상
Parse(String, LoadOptions)
- Source:
- XElement.cs
- Source:
- XElement.cs
- Source:
- XElement.cs
- Source:
- XElement.cs
- Source:
- XElement.cs
XElement 필요에 따라 공백을 유지하고 줄 정보를 유지하는 XML이 포함된 문자열에서 로드합니다.
public:
static System::Xml::Linq::XElement ^ Parse(System::String ^ text, System::Xml::Linq::LoadOptions options);
public static System.Xml.Linq.XElement Parse(string text, System.Xml.Linq.LoadOptions options);
static member Parse : string * System.Xml.Linq.LoadOptions -> System.Xml.Linq.XElement
Public Shared Function Parse (text As String, options As LoadOptions) As XElement
매개 변수
- options
- LoadOptions
공백 동작 및 기본 URI 및 줄 정보를 로드할지 여부를 지정하는 A LoadOptions 입니다.
반품
XElement XML을 포함하는 문자열에서 채워진 값입니다.
예제
다음 예제에서는 문자열 XElement 을 공백 유지 및 공백 유지가 아닌 두 가지 방법으로 구문 분석합니다. 그런 다음 쿼리를 사용하여 결과 XML 트리의 공백 노드 수를 확인합니다.
int whiteSpaceNodes;
XElement xmlTree1 = XElement.Parse("<Root> <Child> </Child> </Root>",
LoadOptions.None);
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);
XElement xmlTree2 = XElement.Parse("<Root> <Child> </Child> </Root>",
LoadOptions.PreserveWhitespace);
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 whiteSpaceNodes As Integer
Dim xmlTree1 As XElement = XElement.Parse("<Root> <Child> </Child> </Root>", LoadOptions.None)
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)
Dim xmlTree2 As XElement = XElement.Parse("<Root> <Child> </Child> </Root>", LoadOptions.PreserveWhitespace)
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
다음 예제에서는 문자열을 구문 분석할 때 줄 정보를 유지합니다.
string markup =
@"<Root>
<Child>
<GrandChild/>
</Child>
</Root>";
XElement xRoot = XElement.Parse(markup, 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>"
Dim xRoot As XElement = XElement.Parse(markup, 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
이 예제는 다음과 같은 출력을 생성합니다.
Element Name Line Position
------------ ---- --------
Root 1 2
Child 2 6
GrandChild 3 10
설명
원본 XML을 들여쓰는 경우 플래그 PreserveWhitespace 를 options 설정하면 판독기가 원본 XML의 모든 공백을 읽습니다. 형식 XText 의 노드는 중요하고 중요하지 않은 공백 모두에 대해 생성됩니다.
원본 XML을 들여쓰는 경우 플래그 PreserveWhitespace 를 options 설정하지 않으면 판독기가 원본 XML의 모든 중요하지 않은 공백을 무시하게 됩니다. XML 트리는 중요하지 않은 공백에 대한 텍스트 노드 없이 만들어집니다.
원본 XML을 들여쓰지 않으면 플래그 PreserveWhitespace 를 options 설정해도 효과가 없습니다. 상당한 공백은 여전히 유지되며, 더 많은 공백 텍스트 노드를 만들 수 있는 중요하지 않은 공백의 범위가 없습니다.
자세한 내용은 XML을 로드하거나 구문 분석하는 동안 공백 유지를 참조하고 직렬화하는 동안 공백을 유지합니다.
에서 구문 분석SetBaseUri할 때 설정 String 은 영향을 주지 않습니다.
XmlReader 유효한 줄 정보가 있을 수 있습니다. 설정하는 SetLineInfo경우 줄 정보는 XML 트리에서 보고 XmlReader되는 줄 정보에서 설정됩니다.
플래그를 설정하면 성능 저하가 SetLineInfo 있습니다.
줄 정보는 XML 문서를 로드한 직후에 정확합니다. 문서를 로드한 후 XML 트리를 수정하면 줄 정보가 의미가 없게 될 수 있습니다.
LINQ to XML의 로드 기능은 기본으로 빌드됩니다 XmlReader. 따라서 오버로드 메서드 및 XmlReader.Create 문서를 읽고 구문 분석하는 메서드에 의해 XmlReader throw되는 예외를 catch할 수 있습니다.