"在解析实体名称&QUOT发生错误;同时加载一个XmlDocument [英] "An error occurred while parsing EntityName" while Loading an XmlDocument

查看:143
本文介绍了"在解析实体名称&QUOT发生错误;同时加载一个XmlDocument的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一些code解析RSS源的ASP.NET C#应用程序,它为所有的RSS工作正常饲料,我都试过了,直到我试图Facebook的。

I have written some code to parse RSS feeds for a ASP.NET C# application and it works fine for all RSS feeds that I have tried, until I tried Facebook.

我的code下面...

My code fails at the last line below...

WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream rss = response.GetResponseStream();
XmlDocument xml = new XmlDocument();
xml.Load(rss);

...出现错误,而解析实体名称,12号线,位置53时发生错误。

...with the error "An error occurred while parsing EntityName. Line 12, position 53."

这是很难弄清什么是XML文件的thhat地位,成为整个文件都在一个线,但它是直接从Facebook和所有字符似乎是EN codeD正确可能除了一个字符(♥)。

It is hard to work out what is at thhat position of the XML file as the entire file is all in one line, but it is straight from Facebook and all characters appear to be encoded properly except possibly one character (♥).

我特别不希望重写我的RSS解析器使用不同的方法。对于如何绕过这个错误有什么建议?有没有关闭文件的检查方式?

I don't particularly want to rewrite my RSS parser to use a different method. Any suggestions for how to bypass this error? Is there a way of turning off checking of the file?

推荐答案

看下载流。它不包含RSS提要,但用约不相容浏览器消息的HTML页面。这是因为下载URL像这样的时候,用户代理头未设置。如果你这样做,你的code应该工作:

Look at the downloaded stream. It doesn't contain the RSS feed, but a HTML page with message about incompatible browser. That's because when downloading the URL like this, the user agent header is not set. If you do that, your code should work:

var request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = "MyApplication";

var xml = new XmlDocument();

using (var response = request.GetResponse())
using (var rss = response.GetResponseStream())
{
    xml.Load(rss);
}

这篇关于"在解析实体名称&QUOT发生错误;同时加载一个XmlDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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