XDocument.Parse 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从字符串创建一个新 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
- 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的任何异常。
另请参阅
适用于
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
一个 LoadOptions 指定空格行为,以及是否加载基本 URI 和行信息。
返回
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,则设置 PreserveWhitespace 标志 options 会导致读取器读取源 XML 中的所有空白。 为重要且微不足道的空白创建类型 XText 节点。
如果源 XML 缩进,则不设置 PreserveWhitespace 标志 options 会导致读取器忽略源 XML 中所有无关紧要的空格。 XML 树是在没有任何文本节点的情况下为微不足道的空白创建的。
如果未缩进源 XML,则 PreserveWhitespace 设置 options 标志不起作用。 仍然保留重要的空白空间,并且没有无关紧要的空白空间,这可能会导致创建更多的空白文本节点。
有关详细信息,请参阅 在加载或分析 XML 时保留空白, 并在 序列化时保留空白。
从 SetBaseUri. 分析时设置String无效。
如果设置 SetLineInfo 标志,则会出现性能损失。
加载 XML 文档后,行信息会立即准确。 如果在加载文档后修改 XML 树,则行信息可能毫无意义。
LINQ to XML 的加载功能基于 XmlReader. 因此,你可能会捕获重载方法和XmlReader.Create读取和分析文档的方法引发XmlReader的任何异常。