Meditation and Mindfulness for Software / IT Professionals. Conducted by Bipin Joshi in Thane. Read more...

Reading and Writing XML Documents in .NET


XML provides platform independent, self-descriptive and very flexible way to represent your data. .NET has harnessed the capabilities of XML in variety of ways. In addition to providing W3C compatible classes .NET also provides additional classes which make XML document manipulation easy. In this article we will take a look at reading and writing XML documents using .NET specific classes.

XML Parsers

To access the content of an XML document you need to parse the document and reach to the specific parts of it. The software which allows you to do that is called as XML parser.

The parsers are of two types:

  • DOM parsers : These parsers built a tree of XML document in memory and allow navigation to various nodes and attributes.
  • SAX parsers : These parsers process XML document in sequential fashion. They have low memory footprint than DOM parsers.

In MSXML Ver.3, Microsoft has provided both of the parsers for our use. In .NET we have DOM parser matching closely with existing MSXML parser. The SAX parser has undergone some variation but provides similar functionality.

The pre-.NET parser i.e. MSXML provided classes and interfaces closely matching W3C recommendations. .NET also follows the same tradition but adds many easy to use and flexible ways to manipulate XML documents.

XML and .NET

XML related classes are available in System.XML namespace. In this article we will primarily discuss about .NET specific classes for reading and writing XML documents.

To read XML documents System.XML provides a class called XmlTextReader. This class is derived from XmlReader and provides easy access to various parts of XML document. This class can be thought as forward-only and read only cursor.

To write XML documents System.XML provides a class called XmlTextWriter. This class is derived from XmlWriter and allows quick writing of XML documents.

The methods provided by these classes are very easy to use and self explaining as compared to W3C DOM . This is mainly because these classes allow various methods to be called directly on themselves. They provide 'current node' for you to manipulate as opposed to W3C DOM classes where you have to track individual nodes. These classes do not require some thing like mydoc.ChildNodes(0).ChildNodes(1).... to access various attributes or content of the node. (You will see examples of using XmlTextReader and XmlTextWriter in later sections)

Using XmlTextReader Class

Now, we will see how to use XmlTextReader class.

  • First import the System.XML namespace. If you are using Visual Studio.NET then Add Reference to System.Xml.dll

e.g. In VB.NET

Imports System.Xml
  • Create instance of XmlTextReader class and load the XML document.

e.g. In VB.NET

dim reader as XmlTextReader
reader = new XmlTextReader ("c:\xml\books.xml")
  • Start reading the XML document

e.g. In VB.NET

do while (reader.Read())
'do some action here

The Read() method returns true if the node is successfully read else returns false. If node is successfully read, you can use properties and methods on the node to perform tasks like checking node type, access attributes, access content of node etc. You can call these methods directly on reader object.

Following table lists important properties of XmlTextReader class.

Property/Method Description
Read() method This method advances the 'cursor' to the next node. Returns true if node is read successfully. You can use this method in a While loop to process the document.
NodeType property This property returns the type of node i.e. Element, text node etc. The type is one from XmlNodeType enumeration.
Name property This property returns name of the current node
Value Property This property returns the content of the node.
ReadString(), Readxxxx().... These Readxxxx() kind of properties return content of node as a specific data type as opposed to value property.
GetAttribute("attb_name") method This method returns string value of given attribute
HasAttributes property This boolean property indicates whether the node has any attributes.

Using XmlTextWriter Class

Now, let us see how to use XmlTextWriter Class.

  • As usual you need to import System.Xml namespace.
  • Specify the file to which XmlTextWriter should write its content

e.g. In VB.NET

dim writer as xmltextwriter
writer=new xmltextwriter("d:\newcatalog.xml",null)

The second argument of the constructor indicates the encoding to be used. The value of null indicates default encoding (ASCII)

Now we can write various parts of XML document into the file. You will be calling various methods on the instance of XmlTextWriter class directly. Following table lists important methods of XmlTextWriter Class.

Property/Method Description
This method writes XML processing instruction to the document i.e. <?xml version="1.0" ?>
This method writes start tag of an element to the document
WriteEndElement() This method is called after calling WriteStartElement() and inserts corresponding end tag in the document
WriteAttribute("attb_name","attb_value") This method is nested with WriteStartElement() and WriteEndElement() methods and writes an attribute to the element
WriteElementString("tag_name","text_in_tag") This method creates a text only elements
Flush() This method flushes document buffer to disk. It is helpful if your document is too big.
Close() This method closes the XML document stream.

Bipin Joshi is a software consultant, an author and a yoga mentor having 21+ years of experience in software development. He conducts online courses in ASP.NET MVC / Core, jQuery, and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced Yoga way of life he also teaches Meditation to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 21 Apr 2001

Tags : .NET Framework XML