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命名空间中。 他们可以很容易地为您解析工作。
我在雅虎发现这个链接非常有用而且简单。 整齐!!!
- 为什么我不能将资源用作具有DataAnnotations的ErrorMessage?
- 从Web服务实例化对象与从常规类实例化对象
- 如何validation列是否存在于DataRow对象中?
- 如何在gridview RowDataBound c#asp.net中创建按钮单击事件
- Asyncfileupload文件预览未显示
- 如何使用DataContractSerializer为generics类型编写自定义序列化程序?
- 如何在.NET Core中实现DbContext连接字符串?
- ConnectionString中未指定OLE DB提供程序。 “提供商= SQLOLEDB;
- AutoMapper从静态API迁移