如何从网址下载xml文件
我想下载按钮或链接点击xml文件,因为我在网页表单中使用Gridview时点击按钮或链接它会打开新标签上的XML文件,因为我想下载它。我现在用的HTTP URL(如:http://SomeName/XmlFiles/1554263.xml)如何从网址下载xml文件
这可能做的伎俩为您
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);
}
}
我已经试过这个,但我需要将它保存在用户提供的特定路径上,所以我想要一个保存对话框并下载该路径,我该如何实现。 –
如何打开此文件的保存对话框下载 –
其不工作,可能是因为保存对话框是winform的属性 –
这可以帮助你。
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());
}
或者,如果重复是不够的 - http://stackoverflow.com/questions/17034396/downloading-xml-file-from-a-url-using-c-sharp显示保存结果文件.. (显然你自己已经完成了这项研究,但是由于某种原因,没有把你的调查结果投入到这篇文章中 - 对于未来的问题,一定要在问题中提供这些信息,否则后期研究可能会由于缺乏*实证研究而被低估*) –