5 Ways to Parse XML in C#

This tutorial illustrates five ways to parse XML in C#. To make the tutorial more user-friendly and easier to understand, actual scripts can be tested and run in your Visual Web Developer environment. I’ll emphasize ASP.NET web development applications/implementations of parsing XML. With little modification of the scripts, however, they can also be tested in a console environment. You can download complete scripts illustrating each of these five ways at the end of the tutorial.

Creating a test web application to parse XML

Follow the steps below to create a test web application to be used in parsing a sample XML document:

  1. Launch Visual Web Developer.
  2. Go to File -> New website.
  3. Select “ASP.NET website” under Visual Studio Templates.
  4. Select “File System,” and under language, select “C#.” Select where in your Windows drive you would like to save your application.
  5. Click OK.
  6. In the Solution explorer, expand Default.aspx until you see Default.aspx.cs.
  7. Throughout this tutorial, you will only be modifying the contents of Default.aspx.cs (the C# script) to learn the various ways of parsing XML.
  8. Download this sample XML document to be used throughout this tutorial: http://www.dotnetdevelopment.net/tutorials/xml_sample_file.zip 

This is what the XML document will look like:

Finally, right click on the zip file and click “Extract here.” Copy the xml_sample_file.xml to the App_Data folder into your test web application. This is how it looks in your Solution Explorer after copying it (and clicking the refresh button):

First way: Using XmlTextReader class

The first method involves using the XmlTextReader class. Source: http://support.microsoft.com/kb/307548

Go to Default.aspx.cs, remove the old code and then replace it with the entire C# code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        XmlTextReader reader = new XmlTextReader(Server.MapPath("~/App_Data/xml_sample_file.xml"));
        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element: // The node is an element.
                    Response.Write("<" + reader.Name + ">");
                    break;
                case XmlNodeType.Text: //Display the text in each element.
                    Response.Write(reader.Value + "<br />");
                    break;
                case XmlNodeType.EndElement: //Display the end of the element.
                    Response.Write("</" + reader.Name + ">");
                    break;
            }
        }

    }
}


Code discussion

First, required namespaces such as System.Xml are imported, and then XmlTextReader class reads the XML document based on the specified location in your server. It will start parsing it by looping using reader.NodeType, and then output the results to the browser.

Here is a screen shot of the parsed XML in the browser (Go to Default.aspx, and then File—>View in Browser):

There are a lot of ways you can load your XML document into the C# parsing script. The one illustrated above, for example, uses Server.MapPath:

XmlTextReader reader = new XmlTextReader(Server.MapPath("~/App_Data/xml_sample_file.xml"));

This approach is convenient and flexible, because if you deploy the web application to any path, it will automatically map the path without the need to manually change the drive paths.

Suppose you use the drive path letters method. This is:

XmlTextReader reader = new XmlTextReader("L:aspdotnetprojectsxmlparse1_csharpApp_Dataxml_sample_file.xml");

You will use the double backslash to prevent path errors. One way you can declare the path without using the double backslash is to use @ before the double quote:

XmlTextReader reader = new XmlTextReader(@"L:aspdotnetprojectsxmlparse1_csharpApp_Dataxml_sample_file.xml");

For the purpose of consistency among the examples illustrated, this tutorial will only use the Server.MapPath("~/App_Data/xml_sample_file.xml") method. You can always experiment with using the other methods when loading the XML document into the C# script.

Second way: Using LINQ to XML methods

This is another popular method of parsing XML in C#. This time it uses Language Integrated Query (LINQ). Source: http://www.mssqltips.com/tip.asp?tip=1524  

Here is the complete Default.aspx.cs C# code:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Xml.Linq;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        var v = from script in XElement.Load(Server.MapPath("~/App_Data/xml_sample_file.xml")).Elements("film")
                select script;
        foreach (var item in v)
        {
            Response.Write(item + "<br />");
        }
    }
}

Code discussion

The important namespaces here that need to be imported are System.Xml.Linq and System.Linq. Then a variable, v, is declared, and the query is constructed, which basically loads the XML document. It will then parse based on what elements need to be displayed (for example, “film”). Once selected, it can then be outputted to the browser. To test, simply replace all of the first method’s code with the code above, and then view in browser. The output should be the same as that of the first method (refer to the screen shot shown previously).

{mospagebreak title=More Ways to Parse XML in C Sharp}

Third way: Using the XmlReader.Create Method

This one is similar to the first method. The only difference is that loading the XML file is done using the XmlReader.Create method (Source: http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.create.aspx) instead of XmlTextReader.

Complete Default.aspx.cs C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.IO;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Set the reader settings.
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.IgnoreComments = true;
        settings.IgnoreProcessingInstructions = true;
        settings.IgnoreWhitespace = true;

        // Create a resolver with default credentials.
        XmlUrlResolver resolver = new XmlUrlResolver();
        resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;

        // Set the reader settings object to use the resolver.
        settings.XmlResolver = resolver;
        XmlReader reader = XmlReader.Create(Server.MapPath("~/App_Data/xml_sample_file.xml"), settings);
        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element: // The node is an element.
                    Response.Write("<" + reader.Name + ">");
                    break;
                case XmlNodeType.Text: //Display the text in each element.
                    Response.Write(reader.Value + "<br />");
                    break;
                case XmlNodeType.EndElement: //Display the end of the element.
                    Response.Write("</" + reader.Name + ">");
                    break;
            }
        }

    }


Code discussion

First, the reader settings are initialized, and the XmlURLresolver is created, which will resolve external XML resources named by a Uniform Resource Identifier (URI). The XML file is then loaded using XmlReader.Create, which will parse and output it to the browser (those looped sections).

Fourth way: Using the XmlDocument.GetElementsByTagName Method

The fourth method uses XmlDocument.GetElementsByTagName. Source: http://msdn.microsoft.com/en-us/library/dc0c9ekk(v=vs.85).aspx  

Complete Default.aspx.cs code C# code:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Xml;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Create the XmlDocument.
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("~/App_Data/xml_sample_file.xml"));

        //Display all movies
        XmlNodeList elemList = doc.GetElementsByTagName("film");
        for (int i = 0; i < elemList.Count; i++)
        {
            Response.Write(elemList[i].InnerXml + "<br />");
        } 
    }
}

Code discussion

First, we create the XmlDocument and load the XmlDocument using doc.Load. Next, all of the XML content is parsed using doc.GetElementsByTagName, which is then looped to be outputted to the browser.

Fifth way: Using Dataset and DataTable Object

The last method to be illustrated is similar to querying a SQL database. It involves using a Data set and data table objects. Source: http://www.csharphelp.com/archives/files/archive32/ReadXML.cs  

Complete Default.aspx.cs code:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Get a DataSet object
        DataSet myDs = new DataSet();

        // Get a FileStream object.
        FileStream myFs =
            new FileStream
               (Server.MapPath("~/App_Data/xml_sample_file.xml"), FileMode.Open, FileAccess.Read);
        // Apply the ReadXml(fileStream method) to read the file
        myDs.ReadXml(myFs);

        // Get a DataTable object from the first table of the DataSet
        DataTable myDt = myDs.Tables[0];

        // Display the values for each row
        foreach (DataRow r in myDt.Rows)
            Response.Write(r[0] + "<br />");
        myFs.Close();
    }
}


Code discussion

The first thing the script does is create a new instance of the DataSet object. Once created, it creates an instance of the FileStream object, which will then open and read the XML document.

To read the XML file, use the ReadXml filestream method. Then treat the contents of the XML file as Tables[0]:  myDs.Tables[0];

Finally, retrieve the contents of the XML file and then loop it to the browser.

Response.Write(r[0] means to get the content of the Table[0], which is basically the XML content of the document.

How to run the scripts

You can download all of the C# scripts here for the first to the fifth method illustrated: http://www.dotnetdevelopment.net/tutorials/parseXML_Csharp_scripts.zip

Extract those scripts and run them in your Visual Web Developer environment. If you plan to run these scripts in the console instead of executing them in a web application, you can follow the steps below.

First, create a new Visual C# console application. Then, in the scripts, replace:

Response.Write

WITH:

Console.Write

Finally, you can read more on creating a Visual C# console application here: http://msdn.microsoft.com/en-us/library/0wc2kk78(v=vs.90).aspx.

One thought on “5 Ways to Parse XML in C#

[gp-comments width="770" linklove="off" ]