All WordPress Themes Available For Free!

Writing XML in C# .NET with XmlTextWriter

A few days ago I started writing application that will write and read settings that are saved in XML file.

So, considering that I have never done this in C# (i’m fairly new to it), I’ve read some documentation and came to the conclusion that XmlTextWriter will be the best for the job. Am I wrong? I could use XmlDocument but since I don’t need to do anything more then simply write XML, I see no reason for using it.

So let’s get dirty. :)

First, add the required reference when working with XML in C#.

using System.Xml;

Now we need to create a new XmlTextWriter instance.

XmlTextWriter writer = new XmlTextWriter("data.xml", null);

data.xml will be the name of the file that will be created, in this case in the same folder as the exe itself.

null means that the default encoding will be used which is UTF-8.

Now we are going to set the indenting.

writer.Formatting = Formatting.Indented;

Now, let’s get to the actual XML elements. First, we need to add declaration to the XML.

writer.WriteStartDocument();

Which will output this:

<?xml version="1.0"?>

Then it is time to add the root element to the XML.

writer.WriteStartElement("users");

After root, we will add a node called profile with an attribute called id that has the value of 10.

writer.WriteStartElement("profile");
writer.WriteAttributeString("id", "10");

By now, we have created this:

<?xml version="1.0"?>
<users>
    <profile id="10">

As you can see, the elements are not closed yet. We will close elements at the end.

Inside profile node, we want to add some child elements. To do this, we simply write this:

writer.WriteElementString("name", "John Doe");
writer.WriteElementString("gender", "male");
writer.WriteElementString("age", "99");

Now let’s close the elements.

//  Close "profile" node
writer.WriteEndElement();

//  Close "users" root element
writer.WriteEndElement();

writer.WriteEndDocument();

And the last step would obviously be to actually save the file. To save it, we just call the .Close() method.

writer.Close();

That’s it. It’s fairly easy to create new XML file with XmlTextWriter.

Here is the whole code and the result.

using System;
using System.Drawing;
using System.Windows.Forms;

//  We need to add XML reference when working with XML
using System.Xml;

namespace XmlTextWriterExample
{
 public partial class Form1 : Form
 {
 public Form1()
 {
 InitializeComponent();
 }

 private void btnWriteXML_Click(object sender, EventArgs e)
 {
 //  Create instance.
 //  "data.xml" will be create in the same folder as the exe.
 //  Setting XmlTextWriter encoding to "null" means that
 //  the file will be written in UTF-8.
 XmlTextWriter writer = new XmlTextWriter("data.xml", null);

 //  Set indenting so that its easier to read XML when open in Notepad and such apps.
 writer.Formatting = Formatting.Indented;

 //  This will output the XML declaration
 writer.WriteStartDocument();

 //  Now let's add root element.
 writer.WriteStartElement("users");

 //  Let's add a node
 writer.WriteStartElement("profile");

 //  We can also add an attribute to "profile" node
 writer.WriteAttributeString("id", "10");

 //  If you want to add children nodes to "profile", here is where you do it.
 //  Between the WriteStartElement("profile") and WriteEndElement()
 writer.WriteElementString("name", "John Doe");
 writer.WriteElementString("gender", "male");
 writer.WriteElementString("age", "99");

 //  Close "profile" node
 writer.WriteEndElement();

 //  Close "users" root element
 writer.WriteEndElement();

 writer.WriteEndDocument();

 //  This will write the above elements to the file itself
 writer.Close();
 }
 }
}

And the XML file itself that we have just created:

<?xml version="1.0"?>
<users>
 <profile id="10">
 <name>John Doe</name>
 <gender>male</gender>
 <age>99</age>
 </profile>
</users>

15 responses on “Writing XML in C# .NET with XmlTextWriter

  1. Pingback: Tweets that mention Writing XML in C# .NET with XmlTextWriter « LessThanWeb -- Topsy.com

  2. Somesh Batra

    Informative article !!! it gives a basic knowledge of reading and writing XML file using C# code. I’ve found an other post related to this over internet during searching time article over internet. It also explained very well, check out this url for more information of this post…..

    http://mindstick.com/Articles/04e767b6-dbb9-4823-bb74-22aa6554b620/?How%20to%20read%20and%20write%20XML%20file%20through%20C%20sharp

    Lastly! I say to thanks everyone for their precious post!

  3. Pingback: Xmltextwriter settings | Susankienitz

  4. Pingback: XML TextWirter « siddi khadar vali pothuganti

  5. Pingback: Sztringműveletek és XML létrehozás C#-ban | Microsoft Innovation Center

  6. Pingback: [RESOLVED]Unable to open it | ASP Questions & Answers

  7. Pingback: [RESOLVED]Unable to open it | ASP Web Form Data Control

Leave a Reply