Wednesday, July 30, 2008

C# code to POST XML Data & Fetch Return data

Hi Friends,

We usually face trouble when we want to fetch the return data after post back, especially when it is an XML Post back. I've developed a code that posts XML data to a website & grabs the return values.Below is the code to POST XML data on a particular website & fetch the return data after form post back.


ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "";
postData += "B2INT0302DEMODEMO"+TextBox1.Text.ToString()+"";
byte[] bBuffer = encoding.GetBytes(postData);

// Prepare web request...
WebRequest myRequest =
myRequest.Method = "POST";
myRequest.ContentType = "application/xml";
myRequest.ContentLength = bBuffer.Length;

// Send the data.
Stream newStream = myRequest.GetRequestStream();
newStream.Write(bBuffer, 0, bBuffer.Length);

myWebResponse = myRequest.GetResponse();

rawResponseStream = myWebResponse.GetResponseStream();

Encoding enc = Encoding.GetEncoding("utf-8");
StreamReader loResponseStream = new StreamReader(myWebResponse.GetResponseStream(), enc);

TextBox2.Text = loResponseStream.ReadToEnd();

XmlDocument xmldoc = new XmlDocument();
XmlNodeList xmlnode1 = xmldoc.GetElementsByTagName("TAG1");
for (int i = 0; i < xmlnode1.Count; i++)

Session["TAG1"] = xmlnode1[i].InnerText;

catch (Exception eee)


Ujjwal B Soni

No comments: