An Easy Guide to Using the XmlTextReader and XmlTextWriter Classes


The .NET framework provides many easy-to-use XML classes. Two of them are the XmlTextReader and XmlTextWriter classes. These classes are very easy to use. Because they don’t require much code, you can easily display the content of an XML file with the XmlTextReader class. The same is also valid for the XmlTextWriter class, except that it will be used to write XML.

Sample Project

The sample project provided with the article contains two examples that will show you the usage of the classes. I have tried to keep the code simple and easy; besides that, it only contains a few lines. When you run the project, you will see two buttons—”Read XML” and “Write XML”. As the name already says, “Read XML” will read an XML file and return the result. Here is what the code looks like to read a file.

Read an XML file example

01:private void btReadXML_Click(object sender, System.EventArgs e)
03:   XmlTextReader xmlReader =
         new XmlTextReader(Server.MapPath("cds.xml"));
05:   // Get rid of the first node, which is
      // <?xml version="1.0" encoding="ISO-8859-1"?>
06:   xmlReader.Read();
08:   Response.Write(xmlReader.AttributeCount);
09:   while(xmlReader.Read())
10:   {
11:      Response.Write(xmlReader.Value);
12:      Response.Write("<br>");
13:   }

As you can see, I am not doing very much in the preceding code. The first line in the function constructs a new XmlTextReader object and loads the XML file. The second line reads the first node, which is <?xml version=”1.0″ encoding=”ISO-8859-1″?>. We do not want to print this line; therefore, we will read it outside the loop and do nothing. Starting with the loop, we read each node and print out the data on the screen, along with a linebreak. That’s all you need to read an XML file. Very easy, isn’t it?

Writing an XML file is also simple. Here is the code to write to an XML file.

Write an XML File Example

01:private void btWriteXML_Click(object sender, System.EventArgs e)
03:   XmlTextWriter xmlWriter =
         new XmlTextWriter(Server.MapPath("friends.xml"), null);
05:   // Format automatically
06:   xmlWriter.Formatting = Formatting.Indented;
08:   // Write root element
09:   xmlWriter.WriteStartElement("FriendsList");
10:   // Write first sub element of root element
11:   xmlWriter.WriteStartElement("Friend");
13:   // now start adding names
14:   xmlWriter.WriteElementString("Name","Michael");
15:   //
16:   xmlWriter.WriteElementString("Age","39");
18:   // write end elements
19:   xmlWriter.WriteEndElement();
20:   // close writer
21:   xmlWriter.Close();
23:   Response.Write("Xml Written");

To write the XML file, we require a little bit more code than to read a file; however, it is still short. The first line of the function constructs a new XmlTextWriter object to write the file. Line 06 uses the Formatting property. This property is used to format the XML file automatically. If we don’t use this property, our XML file will look like this:


To have a clean tree structure look, we have to use the Formatting property. In line 09, we create the FriendsList root element. With the same function, we create a new element, called Friend, in line 10. In lines 14 and 16, we create two new elements with the WriteElementString function. The first parameter in this function is the name of the element and the second parameter is the value of the element. Line 19 writes the end elements and line 21 closes the xmlWriter. That’s it!


In this short article, you have seen how easy it is to read the XML file with the help of the XmlTextReader class. You have also seen how easy it is to write the XML file with the XmlTextWriter class. Both classes do have more functions and properties; all of them are self-explainatory and easy to use. This article was just written to show you how easy the XML .NET classes are.

More by Author

Must Read