Thursday, December 17, 2015

How to save XML from URL c# asp.net

 Here is the example for save XML to your system from the URL.

follow the below to save xml


             public HttpWebRequest GetRequest(string s)
             {
              bool automaticDetectProxy = true;
              var request = (HttpWebRequest)HttpWebRequest.Create(s);
            if (automaticDetectProxy)
            {
               IWebProxy proxy = WebRequest.GetSystemWebProxy();
               System.Net.CredentialCache.DefaultNetworkCredentials.UserName = "Username";
               System.Net.CredentialCache.DefaultNetworkCredentials.Password = "Password";
               System.Net.CredentialCache.DefaultNetworkCredentials.Domain = "Domain";
               proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
               request.Proxy = proxy;
               }
              request.PreAuthenticate = true;
             return request;
         }

              string url = "http://www.xxxx/yyyy
              var request = (HttpWebRequest)this.GetRequest(url);
              
              // //geting the response from the request url
              var response = (HttpWebResponse)request.GetResponse();
              
             // //create a stream to hold the contents of the response (in this case it is the contents of the XML file
              var receiveStream = response.GetResponseStream();
             
               // //creating XML document
              var mySourceDoc = new XmlDocument();
              try
              {
                  // //load the file from the stream
                  mySourceDoc.Load(receiveStream);
                  mySourceDoc.Save("E:\\Logistics\\XML_Gati\\" + tracking + ".xml");
                  // //close the stream
                  receiveStream.Close();
              }
              catch (Exception ex)
              {
                  Response.Write(ex.Message);
              }

No comments:

Post a Comment