Download xml file from web services

by Emanuele 4/3/2008 11:55:00 AM
Sometimes, some network configurations don't allow to access directly to web services.
One way is download the xml file that xml web services returns and load that in a dataset.
I wrote a small function to download xml file from web services.
It's simple, but it's a good starting point to develop other things.


        public bool DownloadXmlFromService()
        {
            string result = "";
            try
            {
                WebProxy proxy = new WebProxy(" proxy address ", port number );
                proxy.Credentials = new NetworkCredential( user id ,  password, domain );
                WebRequest request = WebRequest.Create("http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry?CountryName=Italy");
                request.Proxy = proxy;
                
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                System.IO.Stream stream = response.GetResponseStream();
                System.Text.Encoding ec = System.Text.Encoding.GetEncoding("utf-8");
                System.IO.StreamReader reader = new System.IO.StreamReader(stream, ec);
                char [] chars = new Char[256];
                int count = reader.Read(chars, 0, 256);
                while(count > 0)
                {
                    string str = new String(chars, 0, 256);
                    result = result + str;
                    count = reader.Read(chars, 0, 256);
                }
                response.Close();
                stream.Close();
                reader.Close();
                
                result = result.Replace("&lt;","<");
                result = result.Replace("&gt;",">");
                
                if (File.Exists("temp.xml"))
                {
                    File.Delete("temp.xml");
                }
                
                System.IO.StreamWriter ToFile = new StreamWriter("temp.xml");
                ToFile.Write(result);
                ToFile.Close();
            }
            catch(Exception exp)
            {
                string str = exp.Message;
                return false;
            }
            
            return true;
        }

Related posts



Powered by BlogEngine.NET 1.3.1.0
Theme by Emanuele Bartolesi

About the author

Name of author Emanuele Bartolesi
I'm a senior developer and project manager.

Contact me Contact me

Calendar

<<  December 2008  >>
MoTuWeThFrSaSu
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in

Download Day 2008