Wednesday, September 16, 2009

Downloading xml data from web server and reading in C#

Help URL: http://msdn.microsoft.com/fr-fr/library/system.net.webresponse.getresponsestream(VS.80).aspx

// Create a 'WebRequest' object with the specified url.
WebRequest myWebRequest = WebRequest.Create("http://phoenixswsolutions.com/app.xml");

//Credentials for accessing the information
myWebRequest.Credentails = System.Net.CredentailsCache.DeafultCredentails;

// Send the 'WebRequest' and wait for response.WebResponse myWebResponse = myWebRequest.GetResponse();

// Obtain a 'Stream' object associated with the response object.
Stream ReceiveStream = myWebResponse.GetResponseStream();

Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

// Pipe the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader( ReceiveStream, encode );

string strResponse=readStream.ReadToEnd();

Console.WriteLine(strResponse);

readStream.Close();

// Release the resources of response object.myWebResponse.Close();

No comments: