我怎样才能下载使用C#的XML文件? [英] How can I download an XML file using C#?
本文介绍了我怎样才能下载使用C#的XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
鉴于这种网址:
<一个href="http://www.dreamin$c$c.net/forums/xml.php?showuser=1253">http://www.dreamin$c$c.net/forums/xml.php?showuser=1253
我如何可以下载生成的XML文件并将其加载到内存中,所以我可以使用LINQ抓住从它的信息?
How can I download the resulting XML file and have it loaded to memory so I can grab information from it using Linq?
感谢您的帮助。
推荐答案
加载字符串:
string xml = new WebClient().DownloadString(url);
然后加载到XML:
Then load into XML:
XDocument doc = XDocument.Parse(xml);
例如:
[Test]
public void TestSample()
{
string url = "http://www.dreamincode.net/forums/xml.php?showuser=1253";
string xml;
using (var webClient = new WebClient())
{
xml = webClient.DownloadString(url);
}
XDocument doc = XDocument.Parse(xml);
// in the result profile with id name is 'Nate'
string name = doc.XPathSelectElement("/ipb/profile[id='1253']/name").Value;
Assert.That(name, Is.EqualTo("Nate"));
}
这篇关于我怎样才能下载使用C#的XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文