XmlDocument.PreserveWhitespace 屬性

定義

取得或設定值,指出是否要保留項目內容中的空白字元。

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.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   //Load XML data which includes white space, but ignore
   //any white space in the file.
   XmlDocument^ doc = gcnew 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 );
}

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 是在 trueLoadXml 之前 Load 呼叫,則會保留空白字元節點;否則,如果此屬性是 false ,則會保留大量空白字元,則不會保留空白字元。

如果 PreserveWhitespace 呼叫 true 之前 Save ,則會在輸出中保留檔中的空白字元;否則,如果此屬性為 falseXmlDocument 則會自動縮排輸出。

此方法是檔物件模型 (DOM) Microsoft延伸模組。

適用於