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 之前trueLoad为 或 LoadXml 被调用,则保留空格节点;否则,如果此属性为 false,则保留大量空白,则不保留空格。

trueSave如果 PreserveWhitespace 之前调用 ,则输出中将保留文档中的空白;否则,如果此属性为 falseXmlDocument则会自动缩进输出。

此方法是文档对象模型 (DOM) 的Microsoft扩展。

适用于