Home » Asp.Net » Writing XML with the XmlWriter class

Writing XML with the XmlWriter class

In previous article we have see about writing and reading text document, but now it’s time to start dealing with XML file. Anyhow XML is just a plain text, we can simply start writing an XML tags to the file and save it with .xml file extension. But for .net framework it is very easy and safe to handle.

For writing an XML file .net framework provides two different options.
XMLWriter and XmlDocument, we will see XMLDocument in the next article. In this article we will focus on XMLWriter approach

Let’s see the basic different between XmlWriter and XmlDocument. XMLWriter uses less memory compare to XmlDocument, that is the only 1 issue if we are writing a vey big file. Another difference is if we are modofing an xml file using XmlDocument, we can read an exisitng file, change it as required and update the file while in XMLWriter we need to create the whole document from scratch every time. That’s it, so it is upto user’s preference and situation when which one should be used.

Now lets have a look at the exmple of writing XML file using XmlWriter class.

using System.Xml;

namespace XmlWriterExample
    class Developer
        public int Id { get; set; }
        public string FullName { get; set;  }
        public string Department { get; set; }
        public int Salary { get; set; }

    class Program
        static void Main()
            Developer[] developers = new Developer[2];
            developers[0] = new Developer() {Id= 2, FullName="Tarun Dudhatra", Department = ".Net", Salary=50000 };
            developers[1] = new Developer() { Id = 6, FullName = "Milan Dudhatra", Department = ".Net", Salary = 40000 };

            using (XmlWriter fileWriter = XmlWriter.Create(@"D:\Tarun\developer.xml"))

                foreach (Developer developer in developers)

                    fileWriter.WriteElementString("ID", developer.Id.ToString());
                    fileWriter.WriteElementString("FullName", developer.FullName);
                    fileWriter.WriteElementString("Department", developer.Department);
                    fileWriter.WriteElementString("Salary", developer.Salary.ToString());



The above C sharp code will generate the following XML file:

<?xml version="1.0" encoding="utf-8"?>

		<FullName>Tarun Dudhatra</FullName>
		<FullName>Milan Dudhatra</FullName>

Here first we have created an instace of the XmlWriter class with one parameter (obviously it a path of xml file, where we want to create a xml file).

We have started xml document with WriteStartDocument() method, and then create an element called “developers” this will be converted to in xml file. After that we have created an another element called “Developer” which will be child element of previously created “developers” element. Now we have created 4 child element called Id, Fullname, Department and Salary using WriteElementString() method. We then close the WriteEndElement() method for Developer node and out side the forloop we call that method again for “developers” node. After writing all the element to root element we will end the document by calling WriteEndDocument() method.

That’s simple it takes to generate an xml file using XmlWriter class. In the next blog post we will see how to write an xml file using XmlDocument class.


Download the sample project


Site Admin is a writer of http://www.codingsharp.com website. He is a software professional with hands on 4+ years of experience. Generally he writes an article about Javascript, SQL Server and JQuery. Here he shares his notes, experiences, examples.

About Coding Sharp

Coding sharp is a blog where you can find the authors personal experience, tutorials, examples, code samples, resources related to C#, Asp.Net, Linq, Ajax, MVC, Entity Framework, SQL Server, jQuery, Visual Studio.