Reading XML Files
To read xml files, we can use the XmlReader class. We will use the XML file we created earlier to assign the values into variables. Note that we will simply use variables for storing the values from the XML. A better approach is by creating a Products class the follows the heirarchy of the XML file. The following program demonstrates the use of XmlReader class.
using System;
using System.Xml;
public class Program
{
public static void Main()
{
XmlReader reader = XmlReader.Create("Products.xml");
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element
&& reader.Name == "Product")
{
Console.WriteLine("ID = " + reader.GetAttribute(0));
Console.WriteLine("Name = " + reader.GetAttribute(1));
while (reader.NodeType != XmlNodeType.EndElement)
{
reader.Read();
if (reader.Name == "Price")
{
while (reader.NodeType != XmlNodeType.EndElement)
{
reader.Read();
if (reader.NodeType == XmlNodeType.Text)
{
Console.WriteLine("Price = {0:C}", Double.Parse(reader.Value));
}
}
reader.Read();
} //end if
if (reader.Name == "OtherDetails")
{
while (reader.NodeType != XmlNodeType.EndElement)
{
reader.Read();
if (reader.Name == "BrandName")
{
while (reader.NodeType != XmlNodeType.EndElement)
{
reader.Read();
if (reader.NodeType == XmlNodeType.Text)
{
Console.WriteLine("Brand Name = " + reader.Value);
}
}
reader.Read();
} //end if
if (reader.Name == "Manufacturer")
{
while (reader.NodeType != XmlNodeType.EndElement)
{
reader.Read();
if (reader.NodeType == XmlNodeType.Text)
{
Console.WriteLine("Manufacturer = " + reader.Value);
}
}
} //end if
}
} //end if
} //end while
} //end if
} //end while
}
}
Output:
ID = 001
Name = Soap
Price = $10.00
Brand Name = X Soap
Manufacturer = X Company
There are other ways to read Xml files but this tutorial will focus on using the XmlReader class. First, we create an XmlReader object using the static method Create and passing the filename of the XML file that will be read.
XmlReader reader = XmlReader.Create("Products.xml");
We now enter a loop that will read each node in the xml file including the whitespaces. We use the XmlReader.Read() that returns true if there are more nodes to read or false if there is no more or if it reaches the end of the file.
We first look at the Product element. The first condition checks if the current node read by the reader is an element and has a name of "Product". The XmlNodeTpe enumeration list all the possible node types including whitespaces, text and comments. We then show the values of the attributes of the product element:
Console.WriteLine("ID = " + reader.GetAttribute(0));Console.WriteLine("Name = " + reader.GetAttribute(1));
The GetAttribute() method gets the values of the attribute of an element. The number indicates what attribute you are getting. 0 indicates the first attribute and 1 indicates the second attribute.
You will now encounter a series of nested while and if statements. The first while loop will search for the Price element. The Read() method will go to the next node while it is not an end element. If the type of the node is a text, we display it on the screen.
Basically, reading xml files using XmlReader requires you to look carefully at the structure of your XML.