Retrieving and Processing XML with ASP.Net - The Code and Explanation
(Page 2 of 3 )
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
- 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.
Next: Using the XML Exchanger >>
More ASP.NET Articles
More By Justin Cook