XmlWriterSettings Class

Specifies a set of options to use when creating an XmlWriter instance.

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

Syntax

public sealed class XmlWriterSettings

Remarks

The XmlWriterSettings class allows you to configure various aspects of how an XmlWriter should format and write XML data. This includes settings for indentation, character escaping, conformance to XML standards, and more. You typically create an instance of XmlWriterSettings, configure its properties, and then pass this instance to a method that creates an XmlWriter, such as XmlWriter.Create().

By default, XmlWriterSettings objects are created with settings that produce well-formed XML. You can modify these settings to control specific behaviors, such as:

Constructors

XmlWriterSettings()

Initializes a new instance of the XmlWriterSettings class with default settings.

public XmlWriterSettings();

Properties

ConformanceLevel

Gets or sets a value indicating whether the writer conforms to the XML 1.0 or XML 1.1 recommendation.

public System.Xml.ConformanceLevel ConformanceLevel { get; set; }

Default value: ConformanceLevel.Document

Encoding

Gets or sets the character encoding for the output.

public System.Text.Encoding Encoding { get; set; }

Default value: null (uses the encoding specified by the underlying stream or writer)

Indent

Gets or sets a value indicating whether to format the output with whitespace.

public bool Indent { get; set; }

Default value: false

IndentChars

Gets or sets the characters to use for indentation.

public string IndentChars { get; set; }

Default value: " " (two spaces)

IndentSize

Gets or sets the number of characters to use for indentation.

public int IndentSize { get; set; }

Default value: 0

If IndentSize is greater than 0, it overrides IndentChars.

NewLineChars

Gets or sets the characters to use for line breaks.

public string NewLineChars { get; set; }

Default value: "\r\n" (carriage return and line feed)

NewLineHandling

Gets or sets a value indicating how line breaks are handled.

public System.Xml.NewLineHandling NewLineHandling { get; set; }

Default value: NewLineHandling.Replace

OmitXmlDeclaration

Gets or sets a value indicating whether to omit the XML declaration.

public bool OmitXmlDeclaration { get; set; }

Default value: false

Methods

Clone()

Creates a copy of the current XmlWriterSettings object.

public XmlWriterSettings Clone();

Returns:

Example

The following example demonstrates how to create an XmlWriter with specific formatting settings.


using System;
using System.IO;
using System.Xml;

public class Example
{
    public static void Main(string[] args)
    {
        // Configure settings for pretty printing
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.IndentChars = "    "; // Use 4 spaces for indentation
        settings.NewLineChars = "\n"; // Use Unix-style newlines
        settings.OmitXmlDeclaration = false; // Include XML declaration

        // Use a StringWriter to capture the XML output
        using (StringWriter sw = new StringWriter())
        {
            // Create an XmlWriter with the specified settings
            using (XmlWriter writer = XmlWriter.Create(sw, settings))
            {
                writer.WriteStartDocument(); // Write XML declaration
                writer.WriteStartElement("root");
                writer.WriteAttributeString("version", "1.0");

                writer.WriteStartElement("element1");
                writer.WriteString("Some text content");
                writer.WriteEndElement();

                writer.WriteStartElement("element2");
                writer.WriteAttributeString("id", "abc");
                writer.WriteEndElement();

                writer.WriteEndElement();
                writer.WriteEndDocument();
            }

            Console.WriteLine(sw.ToString());
        }
    }
}
            

Output:


<?xml version="1.0" encoding="utf-16"?>
<root version="1.0">
    <element1>Some text content</element1>
    <element2 id="abc" />
</root>