XDocument.Parse 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從字串建立新 XDocument 資料,可選擇保留空白、設定基礎 URI,並保留行資訊。
多載
| 名稱 | Description |
|---|---|
| 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.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- 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 樹中的空白,可以用 take Parse 的超載LoadOptions作為參數。
欲了解更多資訊,請參閱 載入或解析 XML 時保留空白 ,以及序列 化時保留空白。
LINQ 轉 XML 的載入功能是建立在 XmlReader之上。 因此,你可能會發現超載方法和XmlReader.Create讀取與解析文件的方法拋出XmlReader的任何異常。
另請參閱
適用於
Parse(String, LoadOptions)
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- XDocument.cs
- 來源:
- 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
A 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 轉 XML 的載入功能是建立在 XmlReader之上。 因此,你可能會發現超載方法和XmlReader.Create讀取與解析文件的方法拋出XmlReader的任何異常。