2016-12-27 140 views
0

我想下載按鈕或鏈接點擊xml文件,因爲我在網頁表單中使用Gridview時點擊按鈕或鏈接它會打開新標籤上的XML文件,因爲我想下載它。我現在用的HTTP URL(如:http://SomeName/XmlFiles/1554263.xml如何從網址下載xml文件

+0

或者,如果重複是不夠的 - http://stackoverflow.com/questions/17034396/downloading-xml-file-from-a-url-using-c-sharp顯示保存結果文件.. (顯然你自己已經完成了這項研究,但是由於某種原因,沒有把你的調查結果投入到這篇文章中 - 對於未來的問題,一定要在問題中提供這些信息,否則後期研究可能會由於缺乏*實證研究而被低估*) –

回答

0

這可能做的伎倆爲您

using (System.Net.WebClient client = new System.Net.WebClient()) 
{ 
    client.DownloadFile("http://SomeName/XmlFiles/1554263.xml", "some.xml"); 
} 

WebClient.DownloadFile下載從URI通過在地址參數中指定一個本地文件數據。此方法阻止下載資源。要下載資源並在等待服務器響應時繼續執行,請使用其中一種DownloadFileAsync方法。

編輯

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

我已經試過這個,但我需要將它保存在用戶提供的特定路徑上,所以我想要一個保存對話框並下載該路徑,我該如何實現。 –

+0

如何打開此文件的保存對話框下載 –

+0

其不工作,可能是因爲保存對話框是winform的屬性 –

0

這可以幫助你。

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()); 
}