Jesper Tverskov, June 24, 2008

Create XML with XmlWriter in ASP.NET

In ASP.NET we have three main APIs for creating XML from scratch. We can use the new LINQ to XML, the XmlWriter class or W3C DOM. In this tutorial we look at XmlWriter . The idea is only to show a basic example. There are many more properties and methods and ways of achieving the same result than indicated below. You must look up the proper documentation for all the details.

1. Create products.xml

I use the following XML document, products.xml, in many of my XML tutorials. How could it be created using the XmlWriter class. To get an idea, we will only create the XML Declaration, the top-element and the first product element with attribute and children, and save it to a file.

<?xml version="1.0" encoding="utf-8"?>
<products>
  <product id="p3">
    <nameAlfa</name>
    <price>1200</price>
    <stock>19</stock>
    <country>Germany</country>
  </product>
</products>

At the end of the ASP.NET code shown below, we use Response.Redirect to the new file just to get it loaded in the browser. The code will work right away if copied out of this document and saved with the proper file names.

2. XmlWriter

2.1 create-xml-writer.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="create-xml-writer.aspx.cs" Inherits=" create_xml_writer" %>

2.2 create-xml-writer.aspx.cs

using System;
using System.Xml;
using System.Text;

public partial class create_xml_writer : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    settings.Encoding = Encoding.UTF8;
    
    string xmlDoc = Server.MapPath("products-xmlwriter.xml");

    using (XmlWriter writer = XmlWriter.Create(xmlDoc, settings))
      {
        writer.WriteStartDocument();
        writer.WriteStartElement("products");
          writer.WriteStartElement("product");
            writer.WriteAttributeString("id", "p3");
            writer.WriteElementString("name", "Alpha");
            writer.WriteElementString("price", "1200");
            writer.WriteElementString("stock", "19");
            writer.WriteElementString("country", "Germany");
          writer.WriteEndElement();
        writer.WriteEndElement();
        writer.WriteEndDocument();
      }
    Response.Redirect("products-xmlwriter.xml");  
}

We don’t need the WriteStartDocument if default for XML Declaration is what we want, and if we don’t have a DOCTYPE, PIs or Comments between the XML Declaration and the top-element. Sad that we need to use WriteEndElement() to close an element of ComplexType. In LINQ to XML we only need parenthesis.

Note how clumsy it is to make the XML Declaration. We need to use XmlWriterSettings for encoding, and for encoding we also need the System.Text assembly, and inside the parentheses of WriteStartDocument() we can use “true” and “false” for standalone yes or no. It can easily take an hour to figure the above out the first time.

Updated 2008-07-07