Parsing XML using C#

Consider you have to extract XML based information from a given URL and display it on the web page. This is really useful if you do not want to use iframes and have to customised the appearance of the final output.

Assume we have agreed that the information will look like something as shown in XML below:

<?xml version="1.0" encoding="UTF-8"?> <list> <item1>some item 1</item1> <item2>some item 2</item2> </list>

And some information provider is feeding this information from a URL

Here is how to go about extracting and displaying it.

Step 1: Create a Literal control on the .aspx or .ascx file and set the ‘Text’ property as the URL and ‘Visible’ property as false

<asp:Literal ID="LiteralURLTrackerAddress" Text="" Visible="false" runat="server"></asp:Literal>

You can also add it to web.config if the same URL is used else where in the site and reference it, but please do not hard code it.

Step 2: Add two more Literal controls for each of the list items as below:

 <asp:Literal ID="LiteralItem1" runat="server"></asp:Literal> <asp:Literal ID="LiteralItem2" runat="server"></asp:Literal>

As they are literal controls, you can add any CSS around these controls and set the appearance.

Step 3: In the code behind file, .aspx.cs or .ascx.cs,  add the following code in Page_Load()  or OnLoad() function

Step 4: First we need to create a Request object from the Literal’s text that we set up in step 1

 //create HTTP request
 string URLToSend = LiteralURLTrackerAddress.Text.ToString();
 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URLToSend.ToString()); request.Method = "GET"; request.KeepAlive = true;

Step5: Get a response from the request object

// Get response for http web request
 HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();

Step 6: Read the response in StreamReader

if (webResponse != null) { //Xml response read from a stream; StreamReader responseStream = new StreamReader(webResponse.GetResponseStream());
//get the response string webResponseStream = responseStream.ReadToEnd();

Step 7: As we have agreed that it will be XML format,  parse the response to XML

//parse it into XML
 XmlDocument listXML= new XmlDocument(); listXML.LoadXml(webResponseStream);

Step 8: Read the individual nodes,  your code logic may be different from here onwards and will depend upon your own XML schema.

string baseNode = @"list/"; //basenode
 //actual nodes XmlNode item1 = listXML.SelectSingleNode(baseNode + "item1"); XmlNode item2 = listXML.SelectSingleNode(baseNode + "item2");

Step 9: Assign them to the Literals

 //assign them back LiteralItem1.Text = item1.InnerText.ToString(); LiteralItem2.Text = item2.InnerText.ToString();

Compile and remove any errors.

Add a try / catch block around the code to make it more robust.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s