Breyta

Deila með


XDocument.Parse Method

Definition

Creates a new XDocument from a string, optionally preserving white space, setting the base URI, and retaining line information.

Overloads

Parse(String)

Creates a new XDocument from a string.

Parse(String, LoadOptions)

Creates a new XDocument from a string, optionally preserving white space, setting the base URI, and retaining line information.

Examples

The following example creates a string that contains XML. It then parses the string into an 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)

This example produces the following output:

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

Remarks

This method parses a string and creates an XML tree.

Parse(String)

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

Creates a new XDocument from a string.

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

Parameters

text
String

A string that contains XML.

Returns

An XDocument populated from the string that contains XML.

Examples

The following example creates a string that contains XML. It then parses the string into an 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)

This example produces the following output:

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

Remarks

This method does not preserve white space. If you want to preserve white space in the XML tree, use the overload of Parse that takes LoadOptions as a parameter.

For more information, see Preserve white space while loading or parsing XML and Preserve white space while serializing.

LINQ to XML's loading functionality is built upon XmlReader. Therefore, you might catch any exceptions that are thrown by the XmlReader.Create overload methods and the XmlReader methods that read and parse the document.

See also

Applies to

Parse(String, LoadOptions)

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

Creates a new XDocument from a string, optionally preserving white space, setting the base URI, and retaining line information.

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

Parameters

text
String

A string that contains XML.

options
LoadOptions

A LoadOptions that specifies white space behavior, and whether to load base URI and line information.

Returns

An XDocument populated from the string that contains XML.

Examples

The following example parses a string into an 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())

This example produces the following output:

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

Remarks

If the source XML is indented, setting the PreserveWhitespace flag in options causes the reader to read all white space in the source XML. Nodes of type XText are created for both significant and insignificant white space.

If the source XML is indented, not setting the PreserveWhitespace flag in options causes the reader to ignore all of the insignificant white space in the source XML. The XML tree is created without any text nodes for insignificant white space.

If the source XML is not indented, setting the PreserveWhitespace flag in options has no effect. Significant white space is still preserved, and there are no spans of insignificant white space that could cause the creation of more white space text nodes.

For more information, see Preserve white space while loading or parsing XML and Preserve white space while serializing.

Setting SetBaseUri is not valid when parsing from a String.

There is a performance penalty if you set the SetLineInfo flag.

The line information is accurate immediately after loading the XML document. If you modify the XML tree after loading the document, the line information may become meaningless.

LINQ to XML's loading functionality is built upon XmlReader. Therefore, you might catch any exceptions that are thrown by the XmlReader.Create overload methods and the XmlReader methods that read and parse the document.

See also

Applies to