Reader Level:
ARTICLE

Reading XML Files

Posted by Mahesh Chand Articles | XML March 28, 2001
In this article, I will show you how to use the XmlTextReader class to read an XML document and write data to the console.
  • 0
  • 0
  • 148555
Download Files:
 

The XmlReader and XmlTextReader classes are defined in the System.XML namespace.

The XmlTextReader class is derived from the XmlReader class. The XmlTextReader class can be used to read XML documents. The read function of this document reads the document until the end of its nodes.

In this article, I will show you how to use the XmlTextReader class to read an XML document and write data to the console.

Adding namspace Reference

Since XML classes are defined in the System.XML namespace the first thing you need to do is to add the System.XML reference to the project.

using System.Xml;

Open an XML Document

The constructor of the XmlTextReader class opens an XML file. In this sample, I used an XML file called xmltest.xml in the C\temp directory. You can download the attached file.

// Open an XML file
XmlTextReader reader = new XmlTextReader("C:\\temp\\xmltest.xml");

Reading  Data

The Read method of the XmlTextReader class reads the data.

while ( reader.Read() )
{
Console.WriteLine(reader.Name);
}

Souce Code: Attachment readxml1.cs 1 KB,  xmltest.xml 1 KB

namespace ReadXML
{
using System;
using System.Xml;
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
public Class1()
{
}
public static int Main(string[] args)
{
try
{
// Open an XML file
XmlTextReader reader = new XmlTextReader("C:\\temp\\xmltest.xml");
while ( reader.Read() )
{
Console.WriteLine(reader.Name);
}
}
catch (Exception e)
{
Console.WriteLine ("Exception: {0}", e.ToString());
}
return 0;
}
}
}

Article Extensions
Contents added by Csharpcorner Editor on Dec 17, 2013

As you can see that the XmlTextReader class has been defined above. Now I would like to add some classes for reading the XML File. (There are a couple of classes that are used to read an XML File.)

Let's take a look at a few of the classes and then let's use them as well:

  • XmlDocument
  • XDocument
  • XmlReader
  • StreamReader

XmlDocument

This class is used to represent the XML Document. System.Xml is used to use this class.

Syntax:

XmlDocument MyDoc = new XmlDocument();

MyDoc.Load("C:\\temp\\create.xml");


In the code above  Load() is used to load the XML document from the specified path.

XDocument  

This class accommodates the mandatory details for the XML document. The XDocument derives from the XContainer. The namespace used for XDocument is System.Xml.Linq.

Syntax:

var doc = XDocument.Load("C:\\temp\\create.xml");

In the code above the Load() is used to create a new XDocument instance by using the specified path of XML file.

XmlReader

This class provides noncached, forward-only access to any XML document. The namespace used to access this class is System.Xml.

Syntax:

XmlReader reader = XmlReader.Create("C:\\temp\\create.xml");

In the code above the Create() is used to create a new XmlReader instance with the specified path of the XML file.

StreamReader 

We can use a variable of StreamReader type that can store the XML file which will assign it from File.OpenText.

Syntax:

StreamReader reader = File.OpenText("C:\\temp\\create.xml");

In the above code the reader can store the XML file and we can access the XML file attributes with it.


 

COMMENT USING

Trending up