C# XmlWriter
last modified July 5, 2023
C# XmlWriter tutorial shows how to write XML data in C# with XmlWriter.
XmlWriter
XmlWriter represents a writer that provides a fast, non-cached,
forward-only way to generate streams or files with XML.
The XmlWriter is available in the System.Xml
namespace.
C# XmlWriter example
The following example creates a simple C# XmlWriter.
using System.Xml;
var sts = new XmlWriterSettings()
{
Indent = true,
};
using XmlWriter writer = XmlWriter.Create("data.xml", sts);
writer.WriteStartDocument();
writer.WriteStartElement("value");
writer.WriteValue(6);
writer.WriteEndElement();
writer.WriteEndDocument();
Console.WriteLine("XML document created");
We create a new XML document. It contains the XML declaration and a single XML tag.
var sts = new XmlWriterSettings()
{
Indent = true,
};
using XmlWriter writer = XmlWriter.Create("data.xml", sts);
The XmlWriter is created with the Create method. We
pass the name of the document and the settings to the method.
writer.WriteStartDocument();
The WriteStartDocument method starts a new document.
writer.WriteStartElement("value");
writer.WriteValue(6);
writer.WriteEndElement();
The WriteStartElement and the WriteEndElement pair is
used to add a new element to the document. The WriteValue writes a
single simple-typed value.
writer.WriteEndDocument();
The WriteEndDocument closes any open elements or attributes and
puts the writer back in the start state.
$ dotnet run XML document created $ cat data.xml <?xml version="1.0" encoding="utf-8"?> <value>6</value>
C# XmlWriter example II
The following example creates an XML file with two books.
using System.Xml;
var sts = new XmlWriterSettings()
{
Indent = true,
};
using var writer = XmlWriter.Create("books.xml", sts);
writer.WriteStartDocument();
writer.WriteStartElement("bookstore");
writer.WriteStartElement("book");
writer.WriteAttributeString("genre", "Science Fiction");
writer.WriteStartElement("title");
writer.WriteString("Dune");
writer.WriteEndElement();
writer.WriteStartElement("author");
writer.WriteString("Frank Herbert");
writer.WriteEndElement();
writer.WriteStartElement("price");
writer.WriteString("8.99");
writer.WriteEndElement();
writer.WriteEndElement(); // end of book
writer.WriteStartElement("book");
writer.WriteAttributeString("genre", "Novel");
writer.WriteStartElement("title");
writer.WriteString("Old Goriot");
writer.WriteEndElement();
writer.WriteStartElement("author");
writer.WriteString("Honoré de Balzac");
writer.WriteEndElement();
writer.WriteStartElement("price");
writer.WriteString("9.0");
writer.WriteEndElement();
writer.WriteEndElement(); // end of book
writer.WriteEndDocument();
Console.WriteLine("XML document created");
In addition to XML tags, we also create attributes with
WriteAttributeString.
$ dotnet run
XML document created
$ cat books.xml
<?xml version="1.0" encoding="utf-8"?>
<bookstore>
<book genre="Science Fiction">
<title>Dune</title>
<author>Frank Herbert</author>
<price>8.99</price>
</book>
<book genre="Novel">
<title>Old Goriot</title>
<author>Honoré de Balzac</author>
<price>9.0</price>
</book>
</bookstore>
C# XmlWriter example III
The next example creates an XML file with products.
using System.Xml;
var sts = new XmlWriterSettings()
{
Indent = true,
};
using var writer = XmlWriter.Create("products.xml", sts);
var products = new List<Product>
{
new Product(1, "Product A", 12.2M),
new Product(2, "Product B", 11.3M),
new Product(3, "Product C", 9M),
new Product(4, "Product D", 5.6M),
new Product(5, "Product E", 11.7M)
};
writer.WriteStartDocument();
writer.WriteStartElement("products");
foreach (var product in products)
{
writer.WriteStartElement("product");
writer.WriteStartElement("id");
writer.WriteValue(product.id);
writer.WriteEndElement();
writer.WriteStartElement("name");
writer.WriteValue(product.name);
writer.WriteEndElement();
writer.WriteStartElement("price");
writer.WriteValue(product.price);
writer.WriteEndElement();
writer.WriteEndElement(); // end product
}
writer.WriteEndElement();
writer.WriteEndDocument();
Console.WriteLine("XML document created");
record Product(int id, string name, decimal price);
We have a list of products created with C# record type. We go over the list and generate an XML file from the objects.
$ dotnet run
XML document created
$ cat products.xml
<?xml version="1.0" encoding="utf-8"?>
<products>
<product>
<id>1</id>
<name>Product A</name>
<price>12.2</price>
</product>
<product>
<id>2</id>
<name>Product B</name>
<price>11.3</price>
</product>
<product>
<id>3</id>
<name>Product C</name>
<price>9</price>
</product>
<product>
<id>4</id>
<name>Product D</name>
<price>5.6</price>
</product>
<product>
<id>5</id>
<name>Product E</name>
<price>11.7</price>
</product>
</products>
Source
XmlWriter class - language reference
In this article we have written XML data with XmlWriter.
Author
List all C# tutorials.