Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
XmlDocument Class
 PreserveWhitespace Property
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
XmlDocument..::.PreserveWhitespace Property

Updated: November 2007

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

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)

Visual Basic (Declaration)
Public Property PreserveWhitespace As Boolean
Visual Basic (Usage)
Dim instance As XmlDocument
Dim value As Boolean

value = instance.PreserveWhitespace

instance.PreserveWhitespace = value
C#
public bool PreserveWhitespace { get; set; }
Visual C++
public:
property bool PreserveWhitespace {
    bool get ();
    void set (bool value);
}
J#
/** @property */
public boolean get_PreserveWhitespace()
/** @property */
public  void set_PreserveWhitespace(boolean value)
JScript
public function get PreserveWhitespace () : boolean
public function set PreserveWhitespace (value : boolean)

Property Value

Type: System..::.Boolean

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

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).

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

Visual Basic
Imports System
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

C#
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);

   }
}

Visual C++
#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 );
}


J#
import System.*;
import System.IO.*;
import System.Xml.*;

public class Sample
{
    public static void main(String[] args)
    {
        // Load XML data which includes white space, but ignore
        // any white space in the file.
        XmlDocument doc = new XmlDocument();
        doc.set_PreserveWhitespace(false);
        doc.Load("book.xml");

        // Save the document as is (no white space).
        Console.WriteLine("Display the modified XML...");
        doc.set_PreserveWhitespace(true);
        doc.Save(Console.get_Out());
    } //main 
} //Sample

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>

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker