Lesson 32 Learning to read XML files with the XMLReader class in C#

There are generally 2 methods for reading XML files in C #: Class. xmlDocument Class. XMLReader The XMLDocument class reads the entire contents of an XML file and stores it in system memory. Then to you Allows easy scrolling back and forth within the XML file, even using XPath technology, Do your search or query on the file.


XMLReader class, faster and less memory consuming option on reading files Is XML. The XMLReader class lets you in at any time with just one element Move the contents of the XML file and read the values ​​at the same time, then go to the next element in the file.

By doing the above, it is obvious that the program takes up much less memory, because at any given moment only the amount Will retain the current element. Additionally, because you can manually set the amount of each Check value, you will have direct access to the values ​​you want and this will work very quickly Wet.

In this lesson, focus on working with the XMLReader class, and in the next lesson, teach the class
We will pay Class XMLDocument. For a practical tutorial, let’s work with a small example where an XML file contains exchange rates (We will read rates currency. For this purpose, use an XML file related to the European Central Bank we do. You can download this file and read it from your hard disk.

But in fact both classes XMLReader and XMLDocument class can fine-tune the contents of an XML file from one address The url on a remote server reads like local files and the example XML file can be accessed from + See the address, in the following example code, we will use part of this file:
using System;
using System.Text;
using System.Xml;
namespace ParsingXml
class Program
static void Main (string [] args)
XmlReader xmlReader = XmlReader.Create (“http://www.ecb.int/stats/eurofxref/eurofxrefdaily.xml”);
while (xmlReader.Read ())
if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name ==
if (xmlReader.HasAttributes)
Console.WriteLine (xmlReader.GetAttribute (“currency”) + “:” +
xmlReader.GetAttribute (“rate”));
Console.ReadKey ();

We first created a new version of the Class XMLReader class using the Create () method. This method can receive multiple overloads (parameter sets), but its simplest form is to send a url With the URL of the XML file you want to read. Inside the loop while loop, create the Read () method on the version We call the XMLReader class.

This loop brings the file reader or reader to the next element Moves the file and returns true to the search operation if there is another element Continue. With all the elements of the file and exporting the false value, the loop execution and reading of the file stoppe will be. Within the loop while loop you can find a variety of properties and methods Use the XMLReader class to access information about your current or desired element.

In the example above, first check the NodeType property to make sure the file has an element (tag) and
Also, is its name equal to “Cub” or not? As you can see in the example XML file, any exchange rate
(rate currency) has an element called the Cube tag and that’s exactly what we’re looking for. As soon as we find a Cube element, do a check format to see if our element has Attributes (yode or not) and then by the method () Attribute Get the value of the two properties “currency” and We read “rate”. Finally, we print the values ​​of the two properties in the output and move on to the next element.

The output of the above example code should be a list of available currencies and their rates. As you can see, the process was very simple. Of course this is mostly because we give information to We needed them in the order we read them, without having to do any special operations on them. in other words,
When we display the list of data as a sequential list. In the next lesson, to teach how to read the file
We will pay for XML using the Class XMLDocument class and you will see the difference.

Note: You should be aware that there are many ways to run the above code with bug fixes and
The program will issue an error or exception. Therefore, you should be prepared to manage potential errors, for For this purpose, go to the error management or exception training course in C # language

About Author:

My name is Wajahat Hussain Founder of bestinformativeblog. I welcome you to the Operating System of Blog – bestinformativeblog. I Am An Blogger By Passion. Well experienced for more than 4+ years in SEO, WordPress, Blogging and how to make Passive income, Explore about me by clicking below.