I want to download the xml file on button or link click as i am using Gridview in the web form when click on button or link it will open the xml file on new tab as i want to download it.I am using http url(eg. http://SomeName/XmlFiles/1554263.xml)
            Asked
            
        
        
            Active
            
        
            Viewed 1.3k times
        
    0
            
            
        - 
                    Or if that duplicate is not enough - http://stackoverflow.com/questions/17034396/downloading-xml-file-from-a-url-using-c-sharp shows saving result to file... (obviously you already done this research yourself, but for some reason did not put results of your investigations into the post - for future questions make sure to provide that information in the question. Otherwise post may be downvoted due to lack of *demonstrated research*) – Alexei Levenkov Dec 27 '16 at 06:21
 
2 Answers
1
            This might do the trick for you
using (System.Net.WebClient client = new System.Net.WebClient())
{
    client.DownloadFile("http://SomeName/XmlFiles/1554263.xml", "some.xml");
}
WebClient.DownloadFile downloads to a local file data from the URI specified by in the address parameter. This method blocks while downloading the resource. To download a resource and continue executing while waiting for the server's response, use one of the DownloadFileAsync methods.
Edit
SaveFileDialog savefile = new SaveFileDialog(); 
// set a default file name
savefile.FileName = "unknown.xml";
if (savefile.ShowDialog() == DialogResult.OK)
{
    using (System.Net.WebClient client = new System.Net.WebClient())
    {
        client.DownloadFile("http://SomeName/XmlFiles/1554263.xml", savefile.FileName);
    }
}
        Mohit S
        
- 13,723
 - 6
 - 34
 - 69
 
- 
                    i already tried this but i need to save it on specific path provided by user so i want a save dialog and download at that path,how do i achieve that. – Dinesh Pareek Dec 27 '16 at 05:08
 - 
                    
 - 
                    its not working, may be because Save Dialog is the property of winform – Dinesh Pareek Dec 27 '16 at 07:09
 - 
                    
 
0
            
            
        This might help you.
using System.Net;
string xyzstring;
try
{
    WebClient wc = new WebClient();
    xyzstring= wc.DownloadString("http://www.example.com/somefile.xml");
}
catch (WebException ex)
{
    MessageBox.Show(ex.ToString());
}