MINI SUPPORTERS:

 

 

Reading XML Files Sequentially - Page 2
       by kirupa  |  18 January 2007

The previous page provided you with a brief overview of what XML is and how to read an XML file. This page will translate what you learned and discuss how to get your code to read an XML file!

Basic Approach to Reading XML
The way you read an XML file is similar to using a magnifying glass and looking at each element in the XML file individually. At each element, you determine whether that element has anything valuable to look at, and if it does, you extract the valuable info and move on to the next node.

If you convert the above basic overview into something useful, you will get the following block of code that you can use to read a XML file:

XmlTextReader reader = new XmlTextReader("C:\\links.xml");
while (reader.Read())
{
XmlNodeType nodeType = reader.NodeType;
switch (nodeType)
{
case XmlNodeType.Element:
Console.WriteLine("Element name is {0}", reader.Name);
if (reader.HasAttributes)
{
for (int i = 0; i < reader.AttributeCount; i++)
{
reader.MoveToAttribute(i);
Console.WriteLine("Attribute is {0} with Value {1}: ", reader.Name, reader.Value);
}
}
break;
case XmlNodeType.Text:
Console.WriteLine("Value is: " + reader.Value);
break;
}
}
 

The above code loads an XML file called links.xml, uses a while loop to look at each node, checks whether the node is an element or text, and depending on whether the node is an element or text, does something such as printing something to our console. The interesting tidbits are in the details, so let's look at the code line-by-line.

Looking at the Code
Let me go through each line of the code in greater detail:

XmlTextReader reader = new XmlTextReader("C:\\links.xml"));

The XmlTextReader class is what you primarily use to read data from XML files. In the above line of code, I create a reader object of type XmlTextReader, and I pass the path of my XML file to the constructor.

Notice that I am using two \\ slashes instead of a single \ slash to designate the path. The reason is that a single \ in a string can be interpreted as an escape character. By using two slashes, you avoid having to use the less elegant " and / combination to prevent a Unrecognized Escape Sequence error.

The final thing to note about this line is that if you plan on deploying your application to other users with an embedded links.xml file, be sure to check out my tutorial on how to use resources to internalize links.xml to your situation:

XmlTextReader reader = new XmlTextReader(Assembly.GetExecutingAssembly
()
.GetManifestResourceStream("XMLTest.links.xml"));

Let's move on and look at our while loop:

while (reader.Read())
{
XmlNodeType nodeType = reader.NodeType;
if (nodeType == XmlNodeType.Element)
{
switch(reader.Name) {
case "title":
Console.WriteLine("TITLE: " + reader.ReadString());
break;
case "link":
Console.WriteLine("LINK: " + reader.ReadString());
break;
case "parent":
reader.MoveToAttribute(0);
Console.WriteLine("PARENT: " + reader.Value);
break;
}
}
}

The reader object stores our XML file, and what we need to do is go through each node in our XML file and figure out what it represents. We achieve the "go through each node" goal by using a while loop and using our reader object's Read method.

The reader.Read() statement is a boolean value that returns a true as long as there is data to be read. Once the we reach the end of our XML file, reader.Read() will return a false and the loop terminates.


In the next page, we'll go further in our code and learn what exactly happens when our loop takes us the to the next node.

Onwards to the next page!

1 | 2 | 3

SUPPORTERS:

cloud storage
cloud storage
kirupa.com's fast and reliable hosting provided by Media Temple. Creative web apps. Make your own free flash banners and photo slideshows.
HTML5 CSS3 Mobile Gallery for iPhone, iPad Flash effects. Art without coding.
Flipping Book - page flip flash component. Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery
X-Platform Application Development for Flash Free Flash Components Download - XML Templates, Players and Galleries.

two computer monitors

US Direct

Learn how to advertise on kirupa.com  
 
SHARE:



MINI SUPPORTERS: