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

In the previous page, we started going over the code. In this page, we will pick up from where we left off and tie up any loose ends.


string isbn = ""

Unlike the previous two lines, there is nothing really interesting about this one. That is a good thing, for I figured you would want a break from long, verbose explanations!


if (bookElement.HasAttributes)
{
isbn = bookElement.Attributes["ISBN"].InnerText;
}

With this if statement, I check if our bookElement has any attributes by using the HasAttributes property. Attributes are, like I mentioned earlier, data stored directly on the node element to describe it. In our case, the ISBN information is stored directly on each Book element.

With our data, this check is really unnecessary, for we already know that our node contains an attribute. But, think of this as practice when you do run into XML data that is less consistent than the one used in this tutorial.

On a final note about attributes, there is nothing that attributes can do that cannot be emulated using child elements. Child elements actually provide you with more flexibility to manipulate the data. The reason you are learning this is because in the real-world, you often do not have control over how your incoming data is formatted, so it's good to carry several tools in your tool belt to avoid unexpected surprises.


isbn = bookElement.Attributes["ISBN"].InnerText;

We are finally initializing our isbn variable created earlier. In this line, we use the bookElement's Attributes collection to retrieve our attribute. The attribute can be retrieved by passing in the actual attribute name to your Attributes collection. You then use the InnerText property to retrieve the data stored by your attribute.

You may notice that this looks very similar to our GetElementsByTagName method where you pass in the name of a node and return a list of nodes with that same name. The only difference is that you are not using an index parameter to return just one value from our Attributes collection whereas you did use an index parameter in the GetElementsByTagName case.

The reason for that omission is because you cannot have duplicate attributes with the same name in a node. You only have one attribute with the name you pass in to your Attributes collection, so it would have been unnecessary to specify which attribute you wanted. With GetElementsByTagName, you could have many nodes with the same name, so passing in an index parameter is necessary.


Console.WriteLine("{0} ({1}) is written by {2}\n", title, isbn, author);

In this line I print to the Console all of the data we have accumulated so far. This is pretty straightforward.


Conclusion
If you made it this far, congratulations! You not only were able to create a small application that accesses specific data from an XML file, you also learned a lot about the code that makes all of it work. For an alternative approach to reading XML files, be sure to check out the Reading XML Files Sequentially tutorial.

Just a final word before we wrap up. If you have a question and/or want to be part of a friendly, collaborative community of over 220k other developers like yourself, post on the forums for a quick response!

Kirupa's signature!

 

1 | 2 | 3




SUPPORTERS:

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