This tutorial will show you how to read and extract data from an XML document with C#.NET using the XmlTextReader class in the System.Xml namespace. To view the article on how to write XML documents, please click here

The XML classes in the System.Xml namespace that can be used to read XML documents are:

  • XmlReader
  • XmlTextReader
  • XmlNodeReader
  • There are more readers like XmlDictionaryReader and XmlValidatingReader, but they don’t apply to this tutorial


The code below will provide you with most of the functionality needed to extract data from an XML document. Usually this type of code is used to extract data from an XML RSS feed after which it can be imported to a database. As with writing an XML document (XmlTextWriter and XmlWriter), XmlTextReader and XmlNodeReader are inherited from XmlReader which means XmlReader has a couple of extra features. But for the purpose of this tutorial and in most cases, you’ll only need to use XmlTextReader (or XmlNodeReader).

Code:
<%@ Page Language="C#" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.Xml" %>
<script runat="server">
public void Page_Load(object Src, EventArgs E)
{
	
    //*** Written by user CWA, CoolWebAwards.com Forums. 18 March 2010
    //*** http://forum.coolwebawards.com/
	
	//*** XmlTextReader
	XmlTextReader XmlReader = new XmlTextReader(Server.MapPath("test.xml"));
	
	//*** Uncomment to use XmlReader
	//Dim XmlReader As XmlReader = XmlReader.Create(server.MapPath("test.xml"))
	
	//*** Uncomment to use XmlNodeReader
	//Dim XMLDocument = New XmlDocument
	//XMLDocument.Load(server.MapPath("test.xml"))
	//Dim XmlReader = New XmlNodeReader(XMLDocument)
	
	int intID = 0;
	int intProductCountTotal = 0;
	string strPrice = "";
	string strWeight = "";
	string strDescription = "";
	
	while (XmlReader.Read()) {
	
		//extract the atribute "Id" value when the reader reach the "Product" element.
		if (XmlReader.Name.ToString() == "Product") {
			if (XmlReader.HasAttributes) {
				while (XmlReader.MoveToNextAttribute()) {
					if (XmlReader.Name == "Id") {
						intID = Int32.Parse(XmlReader.Value);
					}
				}
			}
		}
		if (XmlReader.Name.ToString() == "price") strPrice = XmlReader.ReadString().Trim(); 
		if (XmlReader.Name.ToString() == "weight") strWeight = XmlReader.ReadString().Trim(); 
		if (XmlReader.Name.ToString() == "description") strDescription = XmlReader.ReadString().Trim(); 
	
		//When the end element of each <Product> is reached by the XmlReader, you can extract all the values you need and import them into a database or whatever
		//These readers provide forward-only access to XML data, so I choose to extract the data from each of the elements, and then use the values
		if (XmlReader.Name.ToString() == "Product" & XmlReader.NodeType.ToString() == "EndElement") {
	
			Response.Write("Values at product ID " + intID.ToString() + " is:<br>");
			Response.Write("- price: " + strPrice + "<br>");
			Response.Write("- weight: " + strWeight + "<br>");
			Response.Write("- description: " + strDescription + "<br>");
	
			//Reset the values for next product
			intID = 0;
			strPrice = "";
			strWeight = "";
			strDescription = "";
	
			intProductCountTotal = intProductCountTotal + 1;
	
		}
	
	}
	
	XmlReader.Close();
	
	// print the number of <Products> elements counted
	Response.Write("<br>Products in XML doc: " + intProductCountTotal.ToString());	

}
</script>
The XML document info used:

Code:
<!--http://forum.coolwebawards.com-->
<Products>
    <Product Id="1">
        <price>price element text for ID 1</price>
        <weight>weight element text for ID 1</weight>
        <description>description element text for ID 1</description>
	</Product>
    <Product Id="2">
        <price>price element text for ID 2</price>
        <weight>weight element text for ID 2</weight>
        <description>description element text for ID 2</description>
	</Product>
    <Product Id="3">
        <price>price element text for ID 3</price>
        <weight>weight element text for ID 3</weight>
        <description>description element text for ID 3</description>
	</Product>
</Products>
The result when reading the document

Code:
Values at product ID 1 is:
- price: price element text for ID 1
- weight: weight element text for ID 1
- description: description element text for ID 1
Values at product ID 2 is:
- price: price element text for ID 2
- weight: weight element text for ID 2
- description: description element text for ID 2
Values at product ID 3 is:
- price: price element text for ID 3
- weight: weight element text for ID 3
- description: description element text for ID 3

Products in XML doc: 3
Click here for more information on how to write an XML document using C#.NET