Reading XML Files Directly - Page 2
       by kirupa  |  8 May 2007

In the previous page, you were able to create a small application that reads XML data. Most of the work was really in copying and pasting some code, so in this and subsequent pages, you will learn in detail what each line of code does.

The Code
By the end of this article, you should have a good understanding of not only how to create an application that reads XML files, but also be able to know why it works the way it does so that you can make modifications to my basic implementation easily.

Let's get started:

XmlDocument doc = new XmlDocument();

In this line, you are creating a new XmlDocument object called doc. The doc object will be responsible for storing our entire XML file and providing you with easy access to read information from the XML file.


doc.Load("http://www.kirupa.com/net/files/sampleXML.xml");

In this line, we use the doc object's Load method to pass in an XML file. In this case, I am passing in the URL of the XML file, but you can pass in file paths pointing to your hard drive, etc.

So, as of now, we have our XML file loaded into memory thanks to XmlDocument and its Load method. The next step is to actually go through and read the information from memory.


XmlNodeList bookList = doc.GetElementsByTagName("Book");

In the above line, you declare a new variable called bookList that stores a list of all XmlNodes that contain the name "Book". Notice that the type of your variable is XmlNodeList, and you retrieve the list of books by using our doc object's GetElementsByTagName method and passing it the name of the nodes you are looking for.

After this line has executed, your bookList variable will store the four Book nodes (and by extension it's children and attributes) present in our XML file.


foreach (XmlNode node in bookList)
{
XmlElement bookElement = (XmlElement) node;
 
string title = bookElement.GetElementsByTagName("title")[0].InnerText;
string author = bookElement.GetElementsByTagName("author")[0].InnerText;
string isbn = "";
 
if (bookElement.HasAttributes)
{
isbn = bookElement.Attributes["ISBN"].InnerText;
}
 
Console.WriteLine("{0} ({1}) is written by {2}\n", title, isbn, author);
}

Because our bookList variable stores a collection of nodes, we need a way to go through (iterate) the list, pick out each node, and do some more more work on the picked node. To do all of that, like mentioned in the previous sentence, we first need to create a loop structure to iterate through our bookList, and that is what the foreach line in the above code does.

If you are not familiar with foreach, it is a way to go through a collection of data without having to worry about your current index position or extracting the value at that index position. Those two tasks are automatically taken care of, and you specify within the foreach argument the type of the variable you are extracting, a new name, and the data source.

In our code, the type of the variable being extracted is XmlNode, we'll call our new variable of that type node, and the source of the data is in bookList. This means, in the body of your foreach statement, you can use node and any methods exposed by it being an XmlNode.


XmlElement bookElement = (XmlElement) node;

In this line, I am casting our XmlNode object node to an XmlElement called bookElement. A XmlNode object is pretty generic, for there are many pieces of information in an XML file that could be classified as a node. An XML element is a bit more specific, so it provides you with more direct methods to access the data contained inside it.


string title = bookElement.GetElementsByTagName("title")[0].InnerText;
string author = bookElement.GetElementsByTagName("author")[0].InnerText;

In these two lines we store the title and author information for our current node. Notice how we extract that information:

bookElement.GetElementsByTagName("name")[0].InnerText

You use the bookElement object you cast from your node in the earlier line, and you use the familiar GetElementsByTagName method and give it the name of the node you are interested in - either title or author for our case.

At this point, you now have data returned to you in the form of a List. Because there is only one title or author node in bookElement, our list only stores a single node. That means, you can access that single node from the returned list by passing in the index position of the first item, 0.

After having extracted that node, we need to get the text it is storing, and that can be accomplished using the InnerText property.

What I like the most about this line of code is how far the data changes from where it was as an XmlElement to become a string at the end. The following image shows you how each method tagged on to your bookElement changes the type of the data:

How Data Changes


We are almost done looking through the code. There are still a few more lines left, and we'll cover them and wrap this tutorial up on the next page

Onwards to the next page!

1 | 2 | 3




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.