从URL ASP.net加载XML文件 [英] ASP.net load XML file from URL

查看:170
本文介绍了从URL ASP.net加载XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Trying to just simply parse an XML file;

    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。

Data at the root level is invalid. Line 1, position 1.

我必须做一个XMLHTTP请求到该文件第一?还是我正确的假设,我可以从外部URL加载它?

Do I have to do an XMLHTTP request to the file first? Or am I right to assume I can load it in from an external 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字符串,其中负载可以使用URI来获取XML数据。有了您的code,XML解析器实际上是试图解析ADRESS为XML,而不是在URI位置的内容。

LoadXml is waiting for an xml string directly, where Load can use an uri to grab the xml data. With your code, the xml parser was actually trying to parse the adress as xml, not the content at the uri location.

您可以看看.NET Framework的内置的饲料加工类。这些类是在<一个href=\"http://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.aspx\">System.ServiceModel.Syndication命名空间。他们可以为您解析工作很容易。

you may take a look at the builtin feed processing classes of the .Net Framework. These classes are in the System.ServiceModel.Syndication namespace. They can to the parsing job for you quite easily.

这篇关于从URL ASP.net加载XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆