Editare

Partajați prin


XmlDocument.PreserveWhitespace Property

Definition

Gets or sets a value indicating whether to preserve white space in element content.

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

Property Value

true to preserve white space; otherwise false. The default is false.

Examples

The following example shows how to strip white space from a file.

#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

The example uses the file book.xml as input.

<!--sample XML fragment-->
<book genre='novel' ISBN='1-861003-78' misc='sale-item'>
  <title>The Handmaid's Tale</title>
  <price>14.95</price>
</book>

Remarks

This property determines how white space is handled during the load and save process.

If PreserveWhitespace is true before Load or LoadXml is called, white space nodes are preserved; otherwise, if this property is false, significant white space is preserved, white space is not.

If PreserveWhitespace is true before Save is called, white space in the document is preserved in the output; otherwise, if this property is false, XmlDocument auto-indents the output.

This method is a Microsoft extension to the Document Object Model (DOM).

Applies to