ASP.Net Code

Send and Receive XML with ASP.net

This code will enable you to transmit XML to a web service or an application with XML APIs, and receive the XML processed and returned by the server. The code is in C#.


using System;
using System.Net;
using System.Web;
using System.Xml;

public class XMLExchange
{
    // Fields
    private string APIurl;
    private string ProxyString;
    private HttpWebRequest RequestObject;
    private HttpWebResponse ResponseObject;
   
    // Properties
    public string proxy
    {
        get
        {
            return this.ProxyString;
        }
        set
        {
            this.ProxyString = value;
        }
    }

    public string url
    {
        get
        {
            return this.APIurl;
        }
        set
        {
            this.APIurl = value;
        }
    }

    // Methods
    public XMLExchange()
    {
        this.ProxyString = ""; // default proxy if none specified
        this.APIurl = ""; //default web service URL
    }

    public static bool CheckXMLSuccess(XmlTextReader xtr)
    {
        // if returned XML has a node to determine successful send
        bool flag1 = false;
        xtr.WhitespaceHandling = WhitespaceHandling.None;
        while (xtr.Read())
        {
            if (xtr.Name != "SUCCESS")
            {
                continue;
            }
            try
            {
                flag1 = Convert.ToBoolean(xtr.ReadString());
            }
            catch (Exception)
            {
            }
            break;
        }
        return flag1;
    }

    public XmlTextReader sendXML(string XMLtoSend)
    {
        XmlTextReader reader1 = null;
        string text1 = this.APIurl + "?xml=" + HttpUtility.UrlEncode(XMLtoSend);
        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);
        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;
        }
        return reader1;
    }
}
Share

9 Comments

  1. Help,
    I put this code into a asp.net2 project, in the appcode folder with the name of XMLXMLExchange.cs. When I call this from a standard .aspx page I only can call the function of XMLExchange.CheckXMLSuccess how can I call the public XmlTextReader sendXML(string XMLtoSend) command from my page? The intelisence does not show it.

    new to C#
    David

  2. is it in the same namespace?
    I haven't yet tested this with ASP.Net 2.0

  3. Hi Justin,
    Well I assume because it is in a file xmlexchange.cs in the appcode that it would be considered Global namespace to the web application. If I need to put this code in the code behind on that webpage I could but what is the calling command like …

    results = XMLExchange.sendsml("XMLTOSEND") ;

  4. I assume you've instantiated the object?

  5. Humm, not really sure what that means, I guess i have to go back to the books and learn alot more about learn C#. I was really hoping I would not have to go into some massive learning process just to communication with and XML API server.

  6. If an object is static, you can call it directly, like Response.Write()
    If an object or method is not static, you have to instantiate (create an object of that class) before you can call it, like this:
    XMLExchange xe = new XMLExchange();

    then call the methods on the xe object

  7. Hello,
    it all seems to be working now the API is showing it accepted what i sent to it, however I get
    Cannot implicitly convert type 'System.Xml.XmlTextReader' to 'string' when
    TextBox1.Text = xe.sendXML(xmltosend);
    I still have to learn to use the XmlTextReader now.

    Well thanks for your help.

    David

  8. Hi Freinds

    I am using this code in mypage to send xml file but i am not haveing any proxy setting who can i remove this proxy setting and can you please write the step have to send and receive xml.
    your help will be valuable

  9. hi..
    it nice example
    but
    can you plzzz give example of the file that will recive this xml & give response. ??
    plzzzzzzz

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image