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)

從字串建立新的 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 的字串填入的 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 樹狀結構中保留空白字元,請使用 採用 的 多載 Parse LoadOptions 作為參數。

如需詳細資訊,請參閱 在載入或剖析 XML 時保留空白字元 ,以及在 序列化時保留空白字元

LINQ to XML的載入功能建置在 上 XmlReader 。 因此,您可能會攔截多載方法擲回 XmlReader.Create 的任何例外狀況,以及 XmlReader 讀取和剖析檔的方法。

另請參閱

適用於

Parse(String, LoadOptions)

從字串建立新的 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,其指定泛空白字元 (White Space) 的行為,以及是否要載入基底 URI 和行資訊。

傳回

XDocument

從包含 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,在 中 options 設定 PreserveWhitespace 旗標會讓讀取器讀取來源 XML 中的所有空白字元。 類型的 XText 節點會針對重要且不重要的空白字元建立。

如果來源 XML 縮排,但未設定 PreserveWhitespace 中的 options 旗標,會讓讀取器忽略來源 XML 中所有不重要的空白字元。 XML 樹狀結構會建立,而不會有任何不重要的空白字元的文位元組點。

如果來源 XML 未縮排,在 中 options 設定 PreserveWhitespace 旗標不會有任何作用。 仍然會保留重要的空白字元,而且沒有不重要的空白字元範圍,可能會導致建立更多空白字元文位元組點。

如需詳細資訊,請參閱 在載入或剖析 XML 時保留空白字元 ,以及在 序列化時保留空白字元

String 剖析 時,設定 SetBaseUri 無效。

如果您設定 SetLineInfo 旗標,則效能會降低。

載入 XML 檔之後,行資訊會立即正確。 如果您在載入檔之後修改 XML 樹狀結構,行資訊可能會變成無意義。

LINQ to XML的載入功能建置在 上 XmlReader 。 因此,您可能會攔截多載方法擲回 XmlReader.Create 的任何例外狀況,以及 XmlReader 讀取和剖析檔的方法。

另請參閱

適用於