HomeASP.NET Retrieving and Processing XML with ASP.Net
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 Justin Cook Rating: / 8 July 18, 2005
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.
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.
Give your class a name referencing the functionality of the class, not the name of the application or the process which will employ it.
Instantiate objects. We will use both a request and a response object.
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.
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.
The constructors. If no parameters are passed in, the object initializes with the defaults.
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:
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!
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.
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.
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.
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;
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.
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.
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.
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.
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.
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.
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.