Flash Components      Flash Menu      Flash Gallery      Flash Slideshow      FLV Player      Flash Form      MP3 Player      PhotoFlow      Flash CMS      3D Wall      Flash Scroller

Flash / AS

Silverlight

WPF

ASP.net / PHP

Photoshop

Forums

Blog

About

 


FlashComponents
  Galleries
  Slideshows
  Menus
  Design & Effects
  Audio & Video
  User Interface
  Templates

 

 

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


kirupa.com's fast and reliable hosting provided by Media Temple. flash components
The Text Animation Component for Flash CS3
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.
Check out our high quality vector-based design packs! Flash Effect Components
flash menus, buttons and components Digicrafts Components
The best flash components ever! Entheos Flash Website Templates
Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com Purchase & Download Flash Components
flash components Free Website | Make a Website
Streamsolutions Content Delivery Networks Learn how to advertise on kirupa.com