Retrieving and Processing XML with ASP.Net

The .Net Framework includes an extensive set of tools for working with XML data. This article will explain how to retrieve remote XML data from an application that doesn’t necessarily provide Web Services yet, but transmits data in XML format. Also included is the code to process the returned data.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 8
July 18, 2005
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

As we move into a new era of web application development, many sites and applications are providing web services for consumption by our web-based (or otherwise) client applications. However, an enormous number of applications do not provide standardized web services. Many do, however, provide the ability through an API to send a remote request to their system, and receive XML data in return.

A good example of this is if you’re working with a 3rd party, hosted CRM solution or Email Service Provider. You may want to work with the data locally, either in a client application, or even an intranet web application. You have perhaps the unique identifier for a product, or the email address of a user, and you wish to retrieve all of the information for use locally.

The Approach

The approach I decided to take in this case was to create a class primarily for the transmission and retrieval of XML data. This is because I could see that the functionality would come in handy, and I would most likely want to use it in more than one place in my application, on quite possibly even in other applications down the road.

So rather than chopping up the code, I’ll just insert the code for the entire class file and insert comments outlining steps, which I’ll explain afterwards.

The Code and Explanation

namespace MyXMLProject
{
  // step 1
  using System;
  using System.Net;
  using System.Web;
  using System.Xml;

  // step 2
  public class XMLExchanger
  {

    // step 3
    private string APIstring;
    private string ProxyString;
    private HttpWebRequest RequestObject;
    private HttpWebResponse ResponseObject;

    // step 4
    public string proxy
    {
      get
      {
        return this.ProxyString;
      }
      set
      {
        this.ProxyString = value;
      }
    }

    // step 5
    public string url
    {
      get
      {
        return this.APIstring;
      }
      set
      {
        this.APIstring = value;
      }
    }

    // step 6
    public XMLExchanger()
    {
      this.ProxyString = "proxy.host.com:8080";
      this.APIstring = "http://remotesite.com/ ";
    }

    public XMLExchanger(string proxy)
    {
      this.ProxyString = proxy;
      this.APIstring = "http://remotesite.com/ ";
    }
      public XMLExchanger(string proxy, string api)
    {
      this.ProxyString = proxy;
      this.APIstring = api;
    }

    // step 7
    public XmlTextReader transmitXML(string toSend)
    {

      // 7.1
      string text1 = this.APIstring + HttpUtility.UrlEncode(toSend);

      // 7.2
      this.RequestObject = (HttpWebRequest) WebRequest.Create(text1);
      this.RequestObject.Accept = "text/xml";
      this.RequestObject.ContentType = "application/x-www-form-urlencoded";
      this.RequestObject.Method = "GET";
      this.RequestObject.Timeout = 10000;
      this.RequestObject.Proxy = new WebProxy(this.ProxyString);

      // 7.3
      XmlTextReader reader1 = null;
      try
      {
        this.ResponseObject = (HttpWebResponse) this.RequestObject.GetResponse();
        reader1 = new XmlTextReader(this.ResponseObject.GetResponseStream());
      }
      catch (Exception exception1)
      {
        WebException exception2 =
          new WebException(exception1.Message + "
          
(proxy is: " + this.ProxyString + ")");
        
throw exception2;
      }

      // 7.4
      return reader1;
      }
    }
}

Code Explained

  1. Just a quick reminder to make sure that you import the XML system.Xml namespace. This will prevent you from having to retype the reference for every XML purposed object.
  2. Give your class a name referencing the functionality of the class, not the name of the application or the process which will employ it.
  3. Instantiate objects. We will use both a request and a response object.
  4. It’s important to specify the proxy server which the application will need to pass through. This is set as a property rather than hard coded, as you may very likely have a different proxy server in your test environment than in the live host where the application will eventually run.
  5. It’s good to have a default URL which will reference the remote web application or API that you’re retrieving the XML from. This may not be the case however, if you’re reusing the object later in a separate application.
  6. The constructors. If no parameters are passed in, the object initializes with the defaults.
  7. This is the primary method of the class, purely for transmitting XML. (I have extended the class in custom instances to build in error checking, but that was only for specifically formatted XML, it would have to be modified for every different use) For this method, here are the steps:
    1. Here we take any unique text and append it to the default URL. This could be something like ‘userID=123’ or it could even be XML itself!
    2. The request object is instantiated and its properties are initialized. They’re all pretty self explanatory. The POST method can be used in place of GET, but I find that GET is easier to use for debugging purposes. You actually create the request object by calling the static “Create()’ method of WebRequest, and cast it to an HttpWebRequest object.
    3. An XMLTextReader is created, and we attempt to read an XML document from the response generated by the remote system. (the response is first cast into a HttpWebResponse, and then read as XML) If there is a problem, an exception will be thrown. If there is a problem here, it’s most likely in the transmission of the request, not in the remote server’s response. This is because even if the server generates an error, chances are they’ll transmit it in XML format. So you’ll have to create a separate method to examine the returned XML to find any errors there.
    4. Return the XML.

That is it in its purest form. While I’m sure you’ll figure out many bells and whistles that you’d like to add, let’s leave it at that and move on to actually using it in an application.

Using the XML Exchanger

To provide the most basic of examples on which you can feel free to expand to your heart’s content, let’s assume our client application is simply looking to retrieve information about a person. For this person, we have only the user ID, and we know we can query our remote user database through a web API, that will return all the details in XML format. I’ll step through the code, just as I did with the XMLExchanger class.

namespace MyXMLProject
{
  // step 1
  
using System;
  using System.Web.UI;
  using System.Web.UI.HtmlControls;
  using System.Web.UI.WebControls;
  using System.Xml;

  public class WebForm1 : Page
  {

    // step 2
    protected TextBox EmailTxt;
    protected TextBox FirstNameTxt;
    protected TextBox LastNameTxt;
    protected TextBox CompanyTxt;

    private void Page_Load(object sender, EventArgs e)
    {

      if (!IsPostBack)
      {

        // step 3
        int userID;
        try
        {
          userID = Convert.ToInt16(Request.QueryString["id"]);
        }
        catch (Exception)
        {
          userID = 0;
        }

        // step 4
        LoadDataFromXML(GetXML(userID));
      }
    }

    private XmlTextReader GetXML(int userID)
    
{
     
// step 5
     
XMLExchanger exchange1 = new XMLExchanger();
     
exchange1.proxy =
        (Request.ServerVariables["SERVER_NAME"].IndexOf("localhost") == -1)
         ? "http://liveproxy:8080" : "http://testproxy";

      // step 6
      XmlTextReader reader1 = null;
     
try
     
{
       
reader1 = exchange1. transmitXML("?userID=" + userID);
     
}
     
catch (Exception exception1)
     
{
       
// however you want to surface the error
      
}
     
return reader1;
   
}

    // step 7
    
private void LoadDataFromXML(XmlTextReader xtr)
    {
      while (xtr.Read())
      {
        string text2;
        // step 8
        if (xtr.NodeType != XmlNodeType.Element)
        {
          continue;
        }
        string text1 = xtr.Name.ToLower();
        xtr.Read();
        if (xtr.NodeType == XmlNodeType.Text)
        {
          text2 = xtr.ReadString();
          switch (text1)
          {
            case "email":
            {
              this.EmailTxt.Text = text2;
              continue;
            }
            case "firstname":
            {
              this.FirstNameTxt.Text = text2;
              continue;
            }
            case "lastname":
            {
              this.LastNameTxt.Text = text2;
              continue;
             }
             case "company":
             {
               this.CompanyTxt.Text = text2;
               continue;
            }
          }
        }
      }
    }
  }
}

Code Explained

Steps:

  1. Import the namespaces, don’t forget System.Xml!
  2. Declare objects on the ASP.Net page in this case. This will be unnecessary with .Net 2.0 and the advent of partial classes. I’m only reading first and last name, email, and company. You can customize this to your needs.
  3. Here we attempt to pull the user ID out of the query string. If it is not a valid integer value, then you can decide how you want to display an error. For the sake of simplity, I just assigned the value of 0 to move on with the code. You would however opt to prompt the user to input a valid user ID.
  4. Here two steps actually take place on one line. First we call the GetXML method with the ID of the user, which in turn uses our XMLExchanger class. This returns XML, which we pass into the method to load the data into our form fields.
  5. Here’s where we’re actually instantiating the XMLExchanger class, and declaring its properties. There’s a nice line there to switch the proxy depending if you’re running the code in development versus production, therefore you wouldn’t need to manually change the proxy before deploying the application.
  6. Here we call the transmitXML method, passing only the string necessary to get the user info. Therefore the default URL is being used, only the query string parameter is appended. You could even pass through XML in string format here, if that’s what your remote system requires to process a request. An XML reader is returned.
  7. This method steps through the XML, node by node to read the data, and populate the appropriate fields with the pertinent user information. I use ‘while xtr.Read()’ to sequentially read the XML until the end.
  8. Depending on how the data is structured, you may need to modify this to suit your needs. I wanted to read only elements, so I skip everything else. You can set it up to look for attributes, or anything else you need to read.

XML is a very fast, reliable method of transmitting data. No doubt the methods described in this article will be useful to you, they’ve proven themselves essential in a number of applications I’ve created.

blog comments powered by Disqus
ASP.NET ARTICLES

- Implementing ASP.NET 4.0 Page.MetaDescriptio...
- ASP.Net Development Tips
- Intro to Sessions in ASP.Net
- Google Maps API Introduction in ASP.NET usin...
- Creating an ASP.NET 3.5 Gridview Image Galle...
- Encrypt QueryString in ASP.NET 3.5 using VB....
- ASP.NET 3.5 Drop Down List Controls
- Connect to Access Database with ASP.Net
- Secure Audio Streaming with ASP.Net and Flash
- Dynamic Sitemap and Navigation in ASP.Net
- Implement Gzip and Deflate Compression in AS...
- Run ASP.Net in Ubuntu with Apache
- ASP.Net Mono Website Contact Forms
- ASP.Net URL Rewriting Methods
- Murach`s ASP.NET 4 Web Programming with C# 2...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials