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:

<?xml version="1.0" encoding="utf-8"?>

<Students>

  <Student>

    <Name>Abhishek</Name>

    <Location>Dhanbad</Location>

  </Student>

  <Student>

    <Name>Aman</Name>

    <Location>Samastipur</Location>

  </Student>

  <Student>

    <Name>Vicky</Name>

    <Location>Munger</Location>

  </Student>

  <Student>

    <Name>Chandan</Name>

    <Location>Bhagalpur</Location>

  </Student>

  <Student>

    <Name>Ravi</Name>

    <Location>Dhanbad</Location>

  </Student>

</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.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Xml;

 

namespace XMLdemo2

{

    class Program

    {

        static void Main(string[] args)

        {

            // Start with XmlReader object

            //here, we try to setup Stream between the XML file nad xmlReader

            using (XmlReader reader = XmlReader.Create(@"c:\users\gr33n synt4x\documents\visual studio 2010\Projects\XMLdemo2\XMLdemo2\myData.xml"))

            {

                while (reader.Read())

                {

                    if (reader.IsStartElement())

                    {

                        //return only when you have START tag

 

                        switch (reader.Name.ToString())

                        {

                            case "Name":

                                Console.WriteLine("Name of the Element is : " + reader.ReadString());

                                break;

 

                            case "Location":

                                Console.WriteLine("Your Location is : " + reader.ReadString());

                                break;

                        }

                    }

                    Console.WriteLine("");

                }

            }

            Console.ReadKey();

        }

    }

} 

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

Output 

Up Next
    Ebook Download
    View all
    Learn
    View all