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)

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 的字符串。

返回

一个使用包含 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.Create 重载方法和 XmlReader 读取和分析文档的方法引发的任何异常。

另请参阅

适用于

Parse(String, LoadOptions)

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

一个 LoadOptions,指定空白行为以及是否加载基 URI 和行信息。

返回

一个使用包含 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 缩进,将 PreserveWhitespace 标志设置为 会导致 options 读取器读取源 XML 中的所有空格。 类型 XText 为的节点是为重要和微不足道的空白空间创建的。

如果源 XML 缩进,不设置 中的 PreserveWhitespace 标志 options 会导致读取器忽略源 XML 中的所有微不足道的空白。 XML 树在创建时没有任何文本节点,但空白空间微不足道。

如果源 XML 未缩进,则 PreserveWhitespace 设置 中的标志 options 不起作用。 仍保留大量空白,并且没有一段微不足道的空白,这可能会导致创建更多的空白文本节点。

有关详细信息,请参阅 在加载或分析 XML 时保留空白序列化时保留空白

String进行分析时,设置SetBaseUri无效。

如果设置 标志, SetLineInfo 则会造成性能损失。

加载 XML 文档后,行信息立即准确。 如果在加载文档后修改 XML 树,行信息可能会变得毫无意义。

LINQ to XML 的加载功能是基于 构建的 XmlReader。 因此,你可能会捕获由 XmlReader.Create 重载方法和 XmlReader 读取和分析文档的方法引发的任何异常。

另请参阅

适用于