XElement.Parse 方法

定义

从包含 XML 的字符串加载 XElement,还可以选择保留空白和行信息。Load an XElement from a string that contains XML, optionally preserving white space and retaining line information.

重载

Parse(String)

从包含 XML 的字符串加载 XElementLoad an XElement from a string that contains XML.

Parse(String, LoadOptions)

从包含 XML 的字符串加载 XElement,还可以选择保留空白和行信息。Load an XElement from a string that contains XML, optionally preserving white space and retaining line information.

Parse(String)

从包含 XML 的字符串加载 XElementLoad an XElement from a string that contains XML.

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

参数

text
String

一个包含 XML 的 StringA String that contains XML.

返回

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

示例

下面的示例创建一个包含 XML 的字符串。The following example creates a string that contains XML. 然后,将该字符串分析为 XElementIt then parses the string into an XElement.

XElement xmlTree = XElement.Parse("<Root> <Child> </Child> </Root>");  
Console.WriteLine(xmlTree);  
Dim xmlTree As XElement = <Root><Child></Child></Root>  
Console.WriteLine(xmlTree)  

该示例产生下面的输出:This example produces the following output:

<Root>  
  <Child></Child>  
</Root>  

注解

此方法不保留空格。This method does not preserve white space. 如果要保留 XML 树中的空白,请使用采用 LoadOptions 作为参数的 Parse 方法的重载。If you want to preserve white space in the XML tree, use the overload of the Parse method that takes LoadOptions as a parameter. 有关详细信息,请参阅在加载或分析 XML 时保留空白,并在序列化时保留空白区域For more information, see Preserving White Space while Loading or Parsing XML and Preserving White Space While Serializing.

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

另请参阅

Parse(String, LoadOptions)

从包含 XML 的字符串加载 XElement,还可以选择保留空白和行信息。Load an XElement from a string that contains XML, optionally preserving white space and retaining line information.

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

参数

text
String

一个包含 XML 的 StringA String that contains XML.

options
LoadOptions

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

返回

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

示例

下面的示例通过两种不同的方式将字符串分析成 XElement:保留空白,而不保留空格。The following example parses a string into an XElement in two different ways: preserving white space, and not preserving white space. 然后,它使用查询来确定生成的 XML 树中的空白节点数。It then uses a query to determine the number of white space nodes in the resulting XML tree.

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)  

该示例产生下面的输出:This example produces the following output:

Count of white space nodes (not preserving whitespace): 0  
Count of white space nodes (preserving whitespace): 3  

下面的示例在分析字符串时保留行信息。The following example retains line information as it parses the string.

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  

该示例产生下面的输出:This example produces the following output:

Element Name        Line Position  
------------        ---- --------  
Root                1    2  
  Child             2    6  
    GrandChild      3    10  

注解

如果源 XML 已缩进,则在 options 中设置 PreserveWhitespace 标志会导致读取器读取源 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 已缩进,则不设置 options 中的 PreserveWhitespace 标志会导致读取器忽略源 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,则在 options 中设置 PreserveWhitespace 标志不起作用。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 Preserving White Space while Loading or Parsing XML and Preserving White Space While Serializing.

String进行分析时,设置 SetBaseUri 将不起作用。Setting SetBaseUri will have no effect when parsing from a String.

XmlReader 可能具有有效的行信息。The XmlReader may have a valid line information or not. 如果设置 SetLineInfo,则将从 XmlReader报告的行信息在 XML 树中设置行信息。If you set SetLineInfo, the line information will be set in the XML tree from the line information that is reported by the XmlReader.

如果设置了 SetLineInfo 标志,则会降低性能。There 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 的加载功能是基于 XmlReader生成的。LINQ to XML's loading functionality is built upon XmlReader. 因此,您可能会捕获 XmlReader.Create 重载方法引发的任何异常和读取和分析文档的 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.

另请参阅

适用于