XmlWriter Class
Provides a fast, forward-only way to write XML documents or streams.
The XmlWriter
class is an abstract class that provides a high-performance, forward-only implementation for writing XML. It offers a simple API for creating XML documents, supporting features such as element creation, attribute writing, text content, and specialized nodes like comments and processing instructions.
Inheritance
System.Object
System.MarshalByRefObject
System.IO.StreamWriter
System.Xml.XmlWriter
Syntax
public abstract class XmlWriter : MarshalByRefObject, IDisposable
Methods
The XmlWriter
class has numerous methods for writing different types of XML content. Here are some of the most common:
Element Creation
WriteStartElement(string prefix, string localName, string ns)
: Writes a start tag for an element with the specified prefix, local name, and namespace.WriteEndElement()
: Writes the corresponding end tag for the element currently open.WriteFullEndElement()
: Writes a complete end tag for the element currently open.
Attribute Writing
WriteAttributeString(string prefix, string localName, string ns, string value)
: Writes an attribute with the specified name and value.WriteStartAttribute(string prefix, string localName, string ns)
: Writes the start of an attribute.WriteEndAttribute()
: Writes the end of an attribute.
Content Writing
WriteString(string text)
: Writes the specified string.WriteValue(object value)
: Writes the value of the object.WriteComment(string text)
: Writes an XML comment.WriteProcessingInstruction(string name, string text)
: Writes a processing instruction.WriteDocType(string name, string pubid, string sysid, string subset)
: Writes the DOCTYPE declaration.
Convenience Methods
Create(Stream wri ter)
: Creates a newXmlWriter
.Create(Stream wri ter, XmlWriterSettings settings)
: Creates a newXmlWriter
with specified settings.
Properties
XmlWriter
exposes several properties to inspect the current state:
WriteState
: Gets the state of theXmlWriter
.XmlLang
: Gets the current xml:lang scope.XmlSpace
: Gets the current xml:space scope.Settings
: Gets theXmlWriterSettings
used to create this writer.
Example Usage
The following example demonstrates how to create a simple XML document using XmlWriter
:
using System;
using System.Xml;
public class Example
{
public static void Main(string[] args)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true; // Enable indentation for readability
using (XmlWriter writer = XmlWriter.Create("output.xml", settings))
{
writer.WriteStartDocument(); //
writer.WriteStartElement("bookstore"); //
writer.WriteStartElement("book"); //
writer.WriteAttributeString("genre", "fiction");
writer.WriteElementString("title", "The Hitchhiker's Guide to the Galaxy");
writer.WriteElementString("author", "Douglas Adams");
writer.WriteEndElement(); //
writer.WriteStartElement("book"); //
writer.WriteAttributeString("genre", "science fiction");
writer.WriteElementString("title", "Dune");
writer.WriteElementString("author", "Frank Herbert");
writer.WriteEndElement(); //
writer.WriteEndElement(); //
writer.WriteEndDocument(); // End of document
}
Console.WriteLine("XML file 'output.xml' created successfully.");
}
}
using
statement with XmlWriter
to ensure that all resources are properly disposed of and the XML document is flushed to the underlying stream.