c#读取网站xml文件

本文关键字:文件 xml 网站 读取 | 更新日期: 2023-09-27 18:13:29

我有一个函数

string fileToImport="C:'test.xml";
FileInfo fi = new FileInfo(fileToImport);
XmlDocument doc = new XmlDocument();
doc.Load(fileToImport);

这是文件的确切位置

但是我想使用相同的功能并从网站导入xml文件。

例如下面的location

http://testweb:1000/testdir/test.xml

然后我应该打开位置xml文件并以同样的方式输入

FileInfo fi = new FileInfo(fileToImport);
XmlDocument doc = new XmlDocument();
doc.Load(fileToImport);

谁有最好的主意来导入这个?

谢谢,

c#读取网站xml文件

你应该试试

using (var client = new WebClient())
{
    using (var reader = new StringReader(client.DownloadString("http://testweb:1000/testdir/test.xml")))
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(reader);
    }
}

也可以不使用WebClient&StringReader:

XmlDocument doc = new XmlDocument();
doc.Load("http://testweb:1000/testdir/test.xml");

更多信息请访问https://msdn.microsoft.com/de-de/library/system.net.webclient(v=vs.110).aspx