ASP.NET
  Home arrow ASP.NET arrow Page 2 - Retrieving and Processing XML with ASP.Net
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Retrieving and Processing XML with ASP.Net
By: Justin Cook
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2005-07-18

    Table of Contents:
  • Retrieving and Processing XML with ASP.Net
  • The Code and Explanation
  • Using the XML Exchanger

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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

    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.

    More ASP.NET Articles
    More By Justin Cook


     

    ASP.NET ARTICLES

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT