Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
XmlTextWriter Class
 Formatting 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
XmlTextWriter..::.Formatting Property

Updated: November 2007

Indicates how the output is formatted.

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

Visual Basic (Declaration)
Public Property Formatting As Formatting
Visual Basic (Usage)
Dim instance As XmlTextWriter
Dim value As Formatting

value = instance.Formatting

instance.Formatting = value
C#
public Formatting Formatting { get; set; }
Visual C++
public:
property Formatting Formatting {
    Formatting get ();
    void set (Formatting value);
}
J#
/** @property */
public Formatting get_Formatting()
/** @property */
public  void set_Formatting(Formatting value)
JScript
public function get Formatting () : Formatting
public function set Formatting (value : Formatting)

Property Value

Type: System.Xml..::.Formatting

One of the Formatting values. The default is Formatting.None (no special formatting).

Note:

In the .NET Framework version 2.0 release, the recommended practice is to create XmlWriter instances using the XmlWriter..::.Create method and the XmlWriterSettings class. This allows you to take full advantage of all the new features introduced in this release. For more information, see Creating XML Writers.

If the Indented option is set, child elements are indented using the Indentation and IndentChar properties. Only element content is indented. The following C# code writes out HTML elements including mixed content:

 XmlTextWriter w = new XmlTextWriter(Console.Out); 
  w.Formatting = Formatting.Indented; 
  w.WriteStartElement("ol"); 
  w.WriteStartElement("li"); 
  w.WriteString("The big "); // This means "li" now has a mixed content model.
  w.WriteElementString("b", "E"); 
  w.WriteElementString("i", "lephant"); 
  w.WriteString(" walks slowly."); 
  w.WriteEndElement(); 
  w.WriteEndElement();

The above code produces the following output:

 <ol> 
   <li>The big <b>E</b><i>lephant</i> walks slowly.</li> 
 </ol>

When this is viewed in HTML no white space appears between the bold and italic elements. In fact, in this example, if indenting was added between these elements the word "Elephant" would be incorrectly broken.

Note:

Writing any text content, excluding String.Empty puts that element into mixed content mode. Child elements do not inherit this "mixed" mode status. A child element of a "mixed" element does indenting, unless it is also contains "mixed" content. Element content (http://www.w3.org/TR/1998/REC-xml-19980210#sec-element-content) and mixed content (http://www.w3.org/TR/1998/REC-xml-19980210#sec-mixed-content) are defined according to the XML 1.0 definitions of these terms.

The following example writes an XML fragment.

Visual Basic
Option Explicit
Option Strict

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample

    Public Shared Sub Main()
        'Create a writer to write XML to the console.
        Dim writer As XmlTextWriter = Nothing
        writer = New XmlTextWriter(Console.Out)

        'Use indentation for readability.
        writer.Formatting = Formatting.Indented
        writer.Indentation = 4

        'Write an element (this one is the root).
        writer.WriteStartElement("book")

        'Write the title element.
        writer.WriteStartElement("title")
        writer.WriteString("Pride And Prejudice")
        writer.WriteEndElement()

        'Write the close tag for the root element.
        writer.WriteEndElement()

        'Write the XML to file and close the writer.
        writer.Close()
    End Sub 'Main 
End Class 'Sample

C#
using System;
using System.IO;
using System.Xml;

public class Sample
{

  public static void Main()
  {
     //Create a writer to write XML to the console.
     XmlTextWriter writer = null;
     writer = new XmlTextWriter (Console.Out);

     //Use indentation for readability.
     writer.Formatting = Formatting.Indented;
     writer.Indentation = 4;

     //Write an element (this one is the root).
     writer.WriteStartElement("book");

     //Write the title element.
     writer.WriteStartElement("title");
     writer.WriteString("Pride And Prejudice");
     writer.WriteEndElement();

     //Write the close tag for the root element.
     writer.WriteEndElement();

     //Write the XML to file and close the writer.
     writer.Close();  

  }

}

Visual C++
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{

   //Create a writer to write XML to the console.
   XmlTextWriter^ writer = nullptr;
   writer = gcnew XmlTextWriter( Console::Out );

   //Use indentation for readability.
   writer->Formatting = Formatting::Indented;
   writer->Indentation = 4;

   //Write an element (this one is the root).
   writer->WriteStartElement( "book" );

   //Write the title element.
   writer->WriteStartElement( "title" );
   writer->WriteString( "Pride And Prejudice" );
   writer->WriteEndElement();

   //Write the close tag for the root element.
   writer->WriteEndElement();

   //Write the XML to file and close the writer.
   writer->Close();
}


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

public class Sample
{
    public static void main(String[] args)
    {
        //Create a writer to write XML to the console.
        XmlTextWriter writer = null;
        writer = new XmlTextWriter(Console.get_Out());

        //Use indentation for readability.
        writer.set_Formatting(Formatting.Indented);
        writer.set_Indentation(4);

        //Write an element (this one is the root).
        writer.WriteStartElement("book");

        //Write the title element.
        writer.WriteStartElement("title");
        writer.WriteString("Pride And Prejudice");
        writer.WriteEndElement();

        //Write the close tag for the root element.
        writer.WriteEndElement();

        //Write the XML to file and close the writer.
        writer.Close();
    } //main 
} //Sample

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