Share via


How to: Specify the Output Format on the XmlWriter

The XmlWriterSettings class includes several properties that control how the XmlWriter content is formatted.

The following table lists the formatting properties settings on the XmlWriterSettings class.

Property

Description

Encoding

Specifies the text encoding to use. The default is Encoding.UTF8.

Indent

Indicates whether to indent elements. The default is false.

IndentChars

Specifies the character string to use when indenting. The default is two spaces.

NewLineChars

Specifies the character string to use for line breaks. This setting is used when NewLineChars is set to true. The default is \r\n (carriage return, line feed).

NewLineHandling

Specifies how to handle new line characters.

NewLineOnAttributes

Indicates whether to write attributes on a new line. Indent should be set to true when using this property. The default is false.

OmitXmlDeclaration

Indicates whether to write an XML declaration. The default is false.

To indent element nodes

  • The Indent and IndentChars properties control how insignificant white space is formatted.

    Dim settings As New XmlWriterSettings()
    settings.Indent = True
    settings.IndentChars = vbTab
    Dim writer As XmlWriter = XmlWriter.Create("books.xml", settings)
    
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    settings.IndentChars = "\t";
    XmlWriter writer = XmlWriter.Create("books.xml", settings);
    

To write attributes on a separate line

  • Use the NewLineOnAttributes to write each attribute on a new line with one extra level of indentation.

    Dim settings As New XmlWriterSettings()
    settings.Indent = True
    settings.NewLineOnAttributes = True
    Dim writer As XmlWriter = XmlWriter.Create("books.xml", settings)
    
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    settings.NewLineOnAttributes = true;
    XmlWriter writer = XmlWriter.Create("books.xml", settings);
    

See Also

Other Resources

Writing XML with the XmlWriter