BizTalk Utilities CV ,   Jobs ,   Code library
 
Home Page


Add/Edit your code items
Search the code library
Browse for the code library


.NET and XML
How to compare a XML element with XmlTextReader?
How to verify if an element is empty with XmlTextReader and IsEmptyElement?
How to check if the XML elements contains any attributes with XmlTextReader
How to avoid to read the XML declaration with XmlTextReader?
How to create a XML file with XmlTextWriter?
Is it possible to add attributes with XmlTextWriter?
How to Write a Comment to a XML File with XmlTextWriter and WriteComment?
How to add the XML Declaration with XmlTextWriter and WriteStartDocument?
How to use Indentation in XmlTextWriter?
What is the usage of WriteFullEndElement and how do I use?
How to include a CData block with XmlTextWriter and WriteCData?
How to write raw XML with XmlTextWriter?
How to add a namespace with the XmlTextWriter and WriteStartAttribute?
How to create a new xml file with XmlDocument?
How to create a xml on the fly?
How to use the Item property in XmlTextReader?
How to Create a Comment with XmlDocument and XmlComment?
How to Read a XML from a Stream with XmlTextReader?
How to Read a XML from a URL with the XmlTextReader?
How to auto-increment an unique ID in XML with XmlTextReader?


 
 

<< XSLT 


By Sonu Kapoor
First Posted 02/05/2005
Times viewed 497

How to read a XML file using XmlTextReader?


Summary Example to read a XML file with the system.xml.XmlTextReader class.

There are many .NET classes to read an xml file. Here I will show an example about the XmlTextReader class. The XmlTextReader class is a fast forward only cursor to read xml. This class does not provide any validation for you, so you have to make sure that your XML is valid. If you need to validate your xml you can take a look at the XmlValidatingReader class, which I do not cover here. Let's start with our example.

As you can see there is nothing special in the xml file. It is basically a CategoryList which contains many Categories. Each Category contains three sub elements: MainCategory:

This contains the name of the category and an unique ID.

Description:

This is the detailed description of the category.

Active:

A status whether the category is active or not. It can contain either Yes or No

Note:

It is important a have a structed and clean XML so that you wont have any problems when reading it with the XmlTextReader. Remember that it does not validate your xml file. To make sure that it is valid, you can open the file in your web browser and it will validate it for you.

First important thing in our code is to import the System.Xml namespace. This namespace provides the necessary XML classes - in our case the XmlTextReader class. In the second line I have set the page language to C# and set the option "debug" to true (just in the case that something doesn't work the way it should).

Finally in the page_load function I have created a new object of the XmlTextReader class and passing the path of the xml file into the constructor. The Server.MapPath function retrieves the full path of the xml file.

The read function then reads the values from the XML file and moves automatically to the next element. It will return true if it has found an element or false if no elements are found.

The first element of the xml file is the xml declaration, which I do not want to display. So, I just read it and do nothing. After that I start a while loop and read the xml file till no elements are left. In the loop I am calling the property Value which will naturally return the value of the XML element. After the loop we then close the XmlReader to clean up any resources.

Additional information

Further additional information


Rate this article on a scale of 1 to 10 (1 votes, average 4)

Your vote :  

<< XSLT 





Leave a comment for this article
Your name
Your email (optional)
Your comment
Optional: Upload an attachment
Enter the code shown:

 
 

    Email TopXML