Como obter o ficheiro xml do url [duplicado]

[[2] esta pergunta já tem uma resposta aqui:

Quero descarregar o ficheiro xml no botão ou ligar carregue em link, porque estou a usar a grelha no formulário web, quando carregar no botão ou ligar, irá abrir o ficheiro xml na página nova, à medida que o quiser transferir.Estou usando url http (eg. http://SomeName/XmlFiles/1554263.xml)

Author: Dinesh Pareek, 2016-12-27

2 answers

Isto pode servir-te de consolo.
using (System.Net.WebClient client = new System.Net.WebClient())
{
    client.DownloadFile("http://SomeName/XmlFiles/1554263.xml", "some.xml");
}

WebClient.Transfere o ficheiro para um ficheiro local a partir do URI indicado no parâmetro endereço. Este método bloqueia ao transferir o recurso. Para baixar um recurso e continuar a executar enquanto espera pela resposta do servidor, use um dos métodos de download Fileasync.

Editar

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);
    }
}
 0
Author: Mohit Shrivastava, 2016-12-27 06:50:51
Isto pode ajudar-te.
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());
}
 0
Author: , 2016-12-27 05:20:29