Creating XML Writers 

XmlWriter instances are created using the static System.Xml.XmlWriter.Create method.

XmlWriter instances are created using the Create method. The XmlWriterSettings class is used to specify the set of features you want to enable on the new XmlWriter object.

Important

Although the Microsoft .NET Framework includes the XmlTextWriter class, which is a concrete implementation of the XmlWriter class, in version 2.0 of the .NET Framework, the recommended practice is to create XmlWriter instances using the Create method.

Features are enabled or disabled using the properties on the XmlWriterSettings class. You specify which writer features to support by passing an XmlWriterSettings object to the Create method. By using the Create method and the XmlWriterSettings class you get the following benefits:

  • You are able to specify which features you want supported on the created XmlWriter object.

  • The XmlWriterSettings object can be reused to create multiple writer objects. The XmlWriterSettings object is copied and marked read-only for each created writer. Changes to the settings on an XmlWriterSettings instance do not affect existing writers with the same settings. Thus, you can use the same settings to create multiple writers with the same functionality. Or, you can modify the settings on an XmlWriterSettings instance and create a new writer with a different set of features.

  • You can add features to an existing writer. The Create method can accept another XmlWriter object. The underlying XmlWriter object does not have to be a writer created by the static Create method. For example, you can specify a user-defined writer or an XmlTextWriter object, that you want to add additional features to.

  • Take full advantage of all the new features added to the XmlWriter class in this release. There are certain features, such as better conformance checking and compliance to the XML 1.0 recommendation that are available only on XmlWriter objects created by the static Create method.

If an XmlWriterSettings object is not passed to the Create method, the default writer settings are used. The following table lists the default settings on the XmlWriterSettings class.

Property Initial value

CheckCharacters

true

CloseOutput

false

ConformanceLevel

ConformanceLevel.Document

Encoding

Encoding.UTF8

Indent

false

IndentChars

Two spaces

NewLineChars

\r\n (carriage return, new line)

NewLineHandling

NewHandling.Replace

NewLineOnAttributes

false

OmitXmlDeclaration

false

Example

The following example creates an XmlWriter that outputs to an XML file.

Dim settings As New XmlWriterSettings()
settings.Indent = True
settings.IndentChars = "    "
Using writer As XmlWriter = XmlWriter.Create("books.xml", settings)
  ' Write XML data.
  writer.WriteStartElement("book")
  writer.WriteElementString("price", "19.95")
  writer.WriteEndElement()
  writer.Flush()
End Using
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = ("    ");
using (XmlWriter writer = XmlWriter.Create("books.xml", settings)) {
   // Write XML data.
   writer.WriteStartElement("book");
   writer.WriteElementString("price", "19.95");
   writer.WriteEndElement();
   writer.Flush();
}

See Also

Other Resources

XML Documents and Data