Reading XML Files


In this article I will explain about How to Read XML Files with a simple example.

Introduction


The main reason for writing this simple article is so many guys are asking doubts how to read Xml ,how to Write Xml in DotnetSpider Questions section. I Thought this article Will helpful for beginners. You can also find an article about how to write Xml document at
http://www.dotnetspider.com/kb/SubmitSample.aspx?ArticleId=2066

System.Xml namespace contains the XmlReader and XmlTextReader.
The XmlTextReader class is derived from XmlReader class. The XmlTextReader class can be used to read the XML documents. The read function of this document reads the document until end of its nodes.
Using the XmlTextReader class you get a forward only stream of XML data j It is then possible to handle each element as you read it without holding the entire DOM in memory.
XmlTextReader provides direct parsing and tokenizing of XML and implements the XML 1.0 specifications

This article explains how to read an Xml file.

Adding NameSpace as Reference


The first step in the process of reading Xml file is to add System.Xml namespace as reference to our project ,since System.Xml namespace contains the XmlReader and XmlTextReader.




using System.Xml;



let us assume there is an Xml file in c:\Dir\XmlExample.Xml

Open an Xml Document


Create an instance of an XmlTextReader object, and populate it with the XML file. Typically, the XmlTextReader class is used if you need to access the XML as raw data without the overhead of a DOM; thus, the XmlTextReader class provides a faster mechanism for reading XML.




XmlTextReader MyReader = new XmlTextReader("c:\\dir\\XmlExample.Xml");



the above code opens Xml file

Reading Data


The Read method of the XmlTextReader class read the data. See the code



while (MyReader.Read())
{
Response.Write(MyReader.Name);
}


that's all now we are ready to read our Xml file from the above specified directory


Read the XML File into DataSet



You can use the ReadXml method to read XML schema and data into a DataSet. XML data can be read directly from a file, a Stream object, an XmlWriter object, or a TextWriter object.

The code simply looks like the following



string MyXmlFile = @"c:\\Dir\\XmlExample2.xml";
DataSet ds = new DataSet();

System.IO.FileStream MyReadXml = new System.IO.FileStream(MyXmlFile,System.IO.FileMode.Open);
ds.ReadXml(MyReadXml);

DataGrid1.DataSource = ds;
DataGrid1.DataBind();




Thus we can read Xml Data into Dataset .

Summary


This article explained

1. How to read Xml File with example
2. How to read Xml file into DataSet with example.


Comments

Author: prabu27 Sep 2006 Member Level: Silver   Points : 0

Hi! Its Nice yar...


Prabu.T

Author: nitinp14 Nov 2007 Member Level: Bronze   Points : 0

good helpful article.

Author: Prashant Mishra29 Sep 2009 Member Level: Silver   Points : 2

Hi,
This is quite old post,but I am trying it now...
Can it work with .Net 2.0.

Also Iwas trying to get the result. I was using following xml doc
Please access the attached zip file. Believe me it is not containing ny kind of threat




Author: kaleeswaran01 Oct 2009 Member Level: Silver   Points : 1

Hi,
This is nice...

But here u r giving a physical path...

can we give assembly path here?

Regards,
Kaleeswaran,
http://4xchange.blogspot.com



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: