An Easy Guide to Using the XmlTextReader and XmlTextWriter Classes

WEBINAR:On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

Introduction

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.

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.

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!

Conclusion

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.

About the Author

Sonu Kapoor

Sonu Kapoor is an ASP.NET MVP and MCAD. He is the owner of the popular .net website http://dotnetslackers.com. DotNetSlackers publishs the latest .net news and articles - it contains forums and blogs as well. His blog can be seen at: http://dotnetslackers.com/community/blogs/sonukapoor/