XML Output Formatting with XmlTextWriter

XML output formatting with XmlTextWriter consists of several properties that work together to control the output of a document. The output formatting properties are:

  • Formatting
  • IndentChar
  • Indentation
  • QuoteChar

The Formatting property has valid values of None and Indented, with None as the default value. When None is the selected value, the IndentChar and the Indentation properties are ignored, and no formatting occurs. If the Formatting property is set to Indented, the application looks to the Indentation property to see how many IndentChars to write for each level in the hierarchy, and then IndentChars specifies which character to use for indenting. If the Formatting property is set to Indented, the default for Indentation is to write 2 IndentChars for each level in the hierarchy, and the default value for IndentChars is a space. If Formatting is set to Indented, the child elements are indented according to the Indentation and IndentChar values. The indentation performed by the XmlTextWriter depends upon the node type. The nodes that are affected by the Indentation property are:

  • DocumentType
  • Element
  • Comment
  • ProcessingInstruction
  • CDATASection

All other node types are not affected by the Indentation property, and indentation is not performed on them.

The internal subset of the DTD has no indentation or formatting. However, this can be achieved as shown in the code example. The code example does formatting for the DTD internal subset.

String name = "Employees";
String pubid = null;
String sysid = null;
String subset = @"
    <!ELEMENT Employees (Employee)+>
    <!ELEMENT Employee EMPTY>
    <!ATTLIST Employee firstname CDATA #REQUIRED>
    <!ENTITY Company 'Microsoft'>]>
";
XmlTextWriter tw = new XmlTextWriter(Console.Out);
tw.WriteDocType(name, pubid, sysid, subset);

The QuoteChar property determines which character is use to quote attribute values. Valid values are:

  • single quote (&#39;)
  • double quote (&#34;)

The default value for QuoteChar is double quote (&#34;).

The following example writes an XML fragment, setting the Formatting property to Indented, an indentation level of 4, and an indentation character of space (the default).

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();  
  }
}

See Also

Writing XML with the XmlWriter | Well-Formed XML Creation with the XmlTextWriter | Namespace Features within the XmlTextWriter | Customized XML Writer Creation | XmlTextWriter Class | XmlTextWriter Members | XmlWriter Class | XmlWriter Class