Read XML File Using XMLReader in C#

Introduction

In our last article, we started with an introduction to XML and created a physical XML file. Now, we will try to read from an existing XML file using the XmlReader class.

Before proceeding, go through my previous article: Get Started With XML Using C#.

Background

You need an existing XML file to implement this. Except this, we need a basic understanding of C# and XML file structures.

So, my XML file looks like:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Students>  
  3.   <Student>  
  4.     <Name>Abhishek</Name>  
  5.     <Location>Dhanbad</Location>  
  6.   </Student>  
  7.   <Student>  
  8.     <Name>Aman</Name>  
  9.     <Location>Samastipur</Location>  
  10.   </Student>  
  11.   <Student>  
  12.     <Name>Vicky</Name>  
  13.     <Location>Munger</Location>  
  14.   </Student>  
  15.   <Student>  
  16.     <Name>Chandan</Name>  
  17.     <Location>Bhagalpur</Location>  
  18.   </Student>  
  19.   <Student>  
  20.     <Name>Ravi</Name>  
  21.     <Location>Dhanbad</Location>  
  22.   </Student>  
  23. </Students>  
Here, we have Students as the Root element and Student as the parent element for Name and Location.

So, in our code we will try to read data from the XML file.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Xml;  
  6. namespace XMLdemo2  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             // Start with XmlReader object  
  13.             //here, we try to setup Stream between the XML file nad xmlReader  
  14.             using (XmlReader reader = XmlReader.Create(@"c:\users\gr33n synt4x\documents\visual studio 2010\Projects\XMLdemo2\XMLdemo2\myData.xml"))  
  15.             {  
  16.                 while (reader.Read())  
  17.                 {  
  18.                     if (reader.IsStartElement())  
  19.                     {  
  20.                         //return only when you have START tag  
  21.                         switch (reader.Name.ToString())  
  22.                         {  
  23.                             case "Name":  
  24.                                 Console.WriteLine("Name of the Element is : " + reader.ReadString());  
  25.                                 break;  
  26.                             case "Location":  
  27.                                 Console.WriteLine("Your Location is : " + reader.ReadString());  
  28.                                 break;  
  29.                         }  
  30.                     }  
  31.                     Console.WriteLine("");  
  32.                 }  
  33.             }  
  34.             Console.ReadKey();  
  35.         }  
  36.     }  
  37. }   
Explanation

So, we start with the using( ) statement. Inside that, we created a reference of XmlReader. Then assigned a reader stream of a XML file using the Create() method.

Now, we start reading the XML file and reader.Read() returns the Boolean value indicating whether there is a XML statement or not.

If Yes, then we try to check if the current statement contains a starting element or not using reader.IsStartElement().

Since we have a number of different element fields in the Student elements, we are using a switch block. All the cases are names of Element. Since we want an actual text string of the element, for that we used the ReadString(). Since it returns a string type.

Output

XMLReader in C# 


Similar Articles