Writing XML with the XmlWriter class

In previous chapters we have dealt with reading XML, but now it's time to start writing it as well. Since XML is simply text, you could just start writing out XML tags to a file and give it an xml extension, but it's more easy and safer to let the .NET framework handle it, and just like with reading XML, there are at least two different options: The XmlWriter approach and the XmlDocument approach. This article will focus on the first approach, and then we'll look into writing XML with the XmlDocument in the next chapter.

The difference between the two is once again mostly related to memory consumption - XmlWriter uses less memory than XmlDocument, which is only an issue if you write very big files though. Another important difference is that when using XmlDocument, you can read an existing file, manipulate it and then write back the changes. With XmlWriter, you will have to write the entire document from scratch each time. This is not necessarily a problem though, so as always, it really comes down to your situation as well as your personal preferences.

Here's an example of writing XML using the XmlWriter class:
using System;
using System.Text;
using System.Xml;

namespace WritingXml
    class Program
        static void Main(string[] args)
            XmlWriter xmlWriter = XmlWriter.Create("test.xml");


            xmlWriter.WriteAttributeString("age", "42");
            xmlWriter.WriteString("John Doe");

            xmlWriter.WriteAttributeString("age", "39");
            xmlWriter.WriteString("Jane Doe");

The above code will create the following XML:
  <user age="42">John Doe</user>
  <user age="39">Jane Doe</user>
So, we start by creating an instance of the XmlWriter class. It takes at least one parameter, in this case the path to where the XML should be written, but comes in many variations for various purposes. The first thing we should do is to call the WriteStartDocument() method. After that, we write a start element called "users". The XmlWriter will translate this into <users>. Before closing it, we write another start element, "user", which will then become a child of "users". We then proceed to adding an attribute (age) to the element, using the WriteAttributeString() method, and then we write the inner text of the element, by calling the WriteString() method. We then make sure to close the first "user" element with a call to the WriteEndElement() method.

This process is repeated to add another user, except that we don't call WriteEndElement() like we did before. In fact, it should be called twice, since we have an open "users" element too, but the XmlWriter will do that for us when we call the WriteEndDocument() method.

To get the XmlWriter to write our data to the disk, we call the Close() method. You can now open the file, "test.xml", in the same directory where the EXE file of your project is, usually in the bin\debug directory.

And that's really all it takes to write a simple XML file. In the next chapter, we will do the same, but using the XmlDocument class.
^ Top