XDocument.Parse 方法

定义

从字符串创建新 XDocument,还可以选择保留空白和行信息以及设置基 URI。Creates a new XDocument from a string, optionally preserving white space, setting the base URI, and retaining line information.

重载

Parse(String)

从字符串创建新 XDocumentCreates a new XDocument from a string.

Parse(String, LoadOptions)

从字符串创建新 XDocument,还可以选择保留空白和行信息以及设置基 URI。Creates a new XDocument from a string, optionally preserving white space, setting the base URI, and retaining line information.

示例

下面的示例创建一个包含 XML 的字符串。The following example creates a string that contains XML. 然后,将该字符串分析为 XDocumentIt 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>  

注解

此方法分析字符串并创建 XML 树。This method parses a string and creates an XML tree.

Parse(String)

从字符串创建新 XDocumentCreates 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

参数

text
String

包含 XML 的字符串。A string that contains XML.

返回

XDocument

一个使用包含 XML 的字符串填充的 XDocumentAn XDocument populated from the string that contains XML.

示例

下面的示例创建一个包含 XML 的字符串。The following example creates a string that contains XML. 然后,将该字符串分析为 XDocumentIt 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>  

注解

此方法不保留空格。This method does not preserve white space. 如果要保留 XML 树中的空白,请使用作为参数的的重载 Parse LoadOptionsIf you want to preserve white space in the XML tree, use the overload of Parse that takes LoadOptions as a parameter.

有关详细信息,请参阅 在加载或分析 XML 时保留空白 ,并 在序列化时保留空白区域For more information, see Preserve white space while loading or parsing XML and Preserve white space while serializing.

LINQ to XML 的加载功能是基于生成的 XmlReaderLINQ to XML's loading functionality is built upon XmlReader. 因此,你可能会捕获重载方法所引发的任何异常 XmlReader.CreateXmlReader 读取和分析文档的方法。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.

另请参阅

适用于

Parse(String, LoadOptions)

从字符串创建新 XDocument,还可以选择保留空白和行信息以及设置基 URI。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

参数

text
String

包含 XML 的字符串。A string that contains XML.

options
LoadOptions

一个 LoadOptions,指定空白行为以及是否加载基 URI 和行信息。A LoadOptions that specifies white space behavior, and whether to load base URI and line information.

返回

XDocument

一个使用包含 XML 的字符串填充的 XDocumentAn XDocument populated from the string that contains XML.

示例

下面的示例将字符串分析为 XDocumentThe 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  

注解

如果源 XML 已缩进,则 PreserveWhitespace 在中设置标志将 options 导致读取器读取源 xml 中的所有空格。If the source XML is indented, setting the PreserveWhitespace flag in options causes the reader to read all white space in the source XML. XText为有效空白和无意义空白创建类型的节点。Nodes of type XText are created for both significant and insignificant white space.

如果源 XML 已缩进,则未 PreserveWhitespace 在中设置标志将 options 导致读取器忽略源 xml 中所有无意义的空白。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. 创建 XML 树时无需任何文本节点,无意义的空白。The XML tree is created without any text nodes for insignificant white space.

如果未缩进源 XML,则 PreserveWhitespace 在中设置标志 options 不起作用。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.

有关详细信息,请参阅 在加载或分析 XML 时保留空白 ,并 在序列化时保留空白区域For more information, see Preserve white space while loading or parsing XML and Preserve white space while serializing.

SetBaseUri从分析时,设置无效 StringSetting SetBaseUri is not valid when parsing from a String.

如果设置了标志,则会对性能产生负面影响 SetLineInfoThere is a performance penalty if you set the SetLineInfo flag.

行信息在加载 XML 文档后立即准确无误。The line information is accurate immediately after loading the XML document. 如果在加载文档后修改 XML 树,行信息可能会变得毫无意义。If you modify the XML tree after loading the document, the line information may become meaningless.

LINQ to XML 的加载功能是基于生成的 XmlReaderLINQ to XML's loading functionality is built upon XmlReader. 因此,你可能会捕获重载方法所引发的任何异常 XmlReader.CreateXmlReader 读取和分析文档的方法。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.

另请参阅

适用于