ASP.net从URL加载XML文件

试图只是简单地解析XML文件;

protected void Page_Load(object sender, EventArgs e) { XmlDocument xdoc = new XmlDocument();//xml doc used for xml parsing xdoc.LoadXml("http://latestpackagingnews.blogspot.com/feeds/posts/default");//loading XML in xml doc XmlNodeList xNodelst = xdoc.DocumentElement.SelectNodes("entry");//reading node so that we can traverse thorugh the XML foreach (XmlNode xNode in xNodelst)//traversing XML { litFeed.Text += "read"; } } 

但我得到:

根级别的数据无效。 第1行,第1位。

我是否必须首先对该文件执行XMLHTTP请求? 或者我是否正确地假设我可以从外部url加载它?

试试这个 :

 protected void Page_Load(object sender, EventArgs e) { XmlDocument xdoc = new XmlDocument();//xml doc used for xml parsing xdoc.Load( "http://latestpackagingnews.blogspot.com/feeds/posts/default" );//loading XML in xml doc XmlNodeList xNodelst = xdoc.DocumentElement.SelectNodes("entry");//reading node so that we can traverse thorugh the XML foreach (XmlNode xNode in xNodelst)//traversing XML { litFeed.Text += "read"; } } 

LoadXml正在直接等待xml字符串,其中Load可以使用uri来获取xml数据。 使用您的代码,xml解析器实际上试图将地址解析为xml,而不是uri位置的内容。

[编辑]您可以查看.Net Framework的内置Feed处理类。 这些类位于System.ServiceModel.Syndication命名空间中。 他们可以很容易地为您解析工作。

我在雅虎发现这个链接非常有用而且简单。 整齐!!​​!

http://developer.yahoo.com/dotnet/howto-xml_cs.html