Windows API Reference

XmlWriter Class

The XmlWriter class provides a fast, forward-only, read-only method for writing XML documents. It ensures that the output is well-formed XML.

Introduction

XmlWriter is designed for scenarios where you need to generate XML programmatically. It abstracts away the complexities of XML syntax, allowing you to focus on the data you want to represent. This class is part of the System.Xml namespace in the .NET Framework and is widely used for data serialization and configuration file generation.

Key Features
Class Members

Methods

Method Name Description
WriteStartDocument() Writes the XML declaration, and optionally the DTD.
WriteEndDocument() Writes the end of the XML document.
WriteStartElement(string prefix, string localName, string ns) Writes a start tag for the specified element.
WriteEndElement() Writes an end tag for the current element.
WriteAttributeString(string prefix, string localName, string ns, string value) Writes an attribute with the specified name and value.
WriteString(string text) Writes text content.
WriteRaw(string data) Writes raw XML markup or text. Use with caution.
Flush() Flushes any buffered data.

Properties

Property Name Description
XmlLang Gets the current xml:lang scope.
XmlSpace Gets the current xml:space scope.
WriteState Gets the state of the XmlWriter.
Example Usage

The following C# code demonstrates how to use XmlWriter to create a simple XML document.


using System;
using System.Xml;

public class XmlWriterExample
{
    public static void Main(string[] args)
    {
        // Create an XmlWriter with default settings
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true; // Indent the output for readability
        settings.IndentChars = "  "; // Use two spaces for indentation

        using (XmlWriter writer = XmlWriter.Create("output.xml", settings))
        {
            writer.WriteStartDocument(); // 

            writer.WriteStartElement("catalog"); // 

            writer.WriteStartElement("book"); // 
            writer.WriteAttributeString("id", "bk101"); // id="bk101"

            writer.WriteElementString("author", "Gambardella, Matthew"); // Gambardella, Matthew
            writer.WriteElementString("title", "XML Developer's Guide"); // XML Developer's Guide
            writer.WriteElementString("genre", "Computer"); // Computer
            writer.WriteElementString("price", "44.95"); // 44.95
            writer.WriteElementString("publish_date", "2000-10-01"); // 2000-10-01
            writer.WriteElementString("description", "An in-depth look at creating applications with XML."); // An in-depth look at creating applications with XML.

            writer.WriteEndElement(); // 

            writer.WriteStartElement("book"); // 
            writer.WriteAttributeString("id", "bk102"); // id="bk102"

            writer.WriteElementString("author", "Ralls, Kim"); // Ralls, Kim
            writer.WriteElementString("title", "Midnight Rain"); // Midnight Rain
            writer.WriteElementString("genre", "Fantasy"); // Fantasy
            writer.WriteElementString("price", "5.95"); // 5.95
            writer.WriteElementString("publish_date", "2000-12-16"); // 2000-12-16
            writer.WriteElementString("description", "A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world."); // ...

            writer.WriteEndElement(); // 

            writer.WriteEndElement(); // 
            writer.WriteEndDocument(); // End of document
        }

        Console.WriteLine("XML file 'output.xml' created successfully.");
    }
}
            
Note: Using WriteRaw can be dangerous if the input data is not trusted, as it does not escape special characters and could lead to malformed XML or security vulnerabilities.
Related Topics