XmlDocument.PreserveWhitespace 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定值,指出是否要保留項目內容中的空白字元。
public:
property bool PreserveWhitespace { bool get(); void set(bool value); };
public bool PreserveWhitespace { get; set; }
member this.PreserveWhitespace : bool with get, set
Public Property PreserveWhitespace As Boolean
屬性值
若要保留空白字元,則為 true;否則為 false。 預設為 false。
範例
下列範例示範如何從檔案中移除空白字元。
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
//Load XML data which includes white space, but ignore
//any white space in the file.
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = false;
doc.Load("book.xml");
//Save the document as is (no white space).
Console.WriteLine("Display the modified XML...");
doc.PreserveWhitespace = true;
doc.Save(Console.Out);
}
}
Imports System.IO
Imports System.Xml
public class Sample
public shared sub Main()
'Load XML data which includes white space, but ignore
'any white space in the file.
Dim doc as XmlDocument = new XmlDocument()
doc.PreserveWhitespace = false
doc.Load("book.xml")
'Save the document as is (no white space).
Console.WriteLine("Display the modified XML...")
doc.PreserveWhitespace = true
doc.Save(Console.Out)
end sub
end class
此範例會使用 檔案 book.xml 作為輸入。
<!--sample XML fragment-->
<book genre='novel' ISBN='1-861003-78' misc='sale-item'>
<title>The Handmaid's Tale</title>
<price>14.95</price>
</book>
備註
此屬性會決定在載入和儲存程式期間如何處理空白字元。
如果 PreserveWhitespace 是在 true 或 LoadXml 之前 Load 呼叫,則會保留空白字元節點;否則,如果此屬性是 false ,則會保留大量空白字元,則不會保留空白字元。
如果 PreserveWhitespace 呼叫 true 之前 Save ,則會在輸出中保留檔中的空白字元;否則,如果此屬性為 false , XmlDocument 則會自動縮排輸出。
此方法是檔物件模型 (DOM) Microsoft延伸模組。