C# 联合RSS阅读器因XML无效而失败?
我已经编写了一段代码,它使用C# 联合RSS阅读器因XML无效而失败?,c#,.net,xml,rss,syndication,C#,.net,Xml,Rss,Syndication,我已经编写了一段代码,它使用System.ServiceModel.Syndication库解析RSS提要 问题是,对于我的一个提要(由facebook提供),我在响应的最后得到了以下行,联合库无法解析提要,因为它说文本是无效的XML,它说这是因为这一部分: ... </channel> <access:restriction relationship="deny" xmlns:access="http://www.bloglines.com/about/specs
System.ServiceModel.Syndication
库解析RSS提要
问题是,对于我的一个提要(由facebook提供),我在响应的最后得到了以下行,联合库无法解析提要,因为它说文本是无效的XML,它说这是因为这一部分:
...
</channel>
<access:restriction relationship="deny" xmlns:access="http://www.bloglines.com/about/specs/fac-1.0" />
</rss>
我得到的例外是:
System.Xml.XmlException: 'Element' is an invalid XmlNodeType. Line 282, position 4.
在System.Xml.XmlReader.ReadEndElement()…上,SyndicationFeed似乎与facebook使用的access:restriction元素有问题。请参阅上的最新线程 Michael Sun(MSFT)写道:“刚刚看到Martin的帖子!非常有帮助!我也做了一些关于这个问题的研究。这个元素来自Bloglines,。听起来好像facebook正在为其RSS 2.0订阅源使用一个扩展。从这篇文章来看,Rss20FeedFormatter似乎不是唯一一个不支持这些元素的 我同意Martin使用XDocument(LINQ到XML)解析RSS提要。或者,如果您正在通过C#构建一些大型应用程序,Facebook C#SDK也会很有帮助。” 编辑:
然而,Atomfeed似乎没有遇到这个问题。这么简单的解决方案 将使用此链接()。因此,将format参数从rss20更改为atom10
另一个替代方法是,通过在通道关闭标签之后跳过任何元素来编写一个继承的XMLTEnguriver,通过跳过通道关闭标签之后的任何元素。(请注意下面的代码没有任何保证,因为我认为自己仍然是一个新手C开发人员。请随时纠正任何可能的错误)
也许这篇文章可以帮助你[link]connect.microsoft.com/VisualStudio/feedback/details/325421/syndicationfeed-load-fails-to-parse-datetime-frost-a-real-world-feeds-ie7-can-read这是非常有用的信息,但是使用XDocument解析确实很痛苦,因为那样我就需要分别支持ATOM、RSS等等。。。您能想到的任何其他解决方案?也许您可以加载XML,删除无效的访问标记,然后使用SyndicationFeed重新加载?尝试将格式更改为atom10,这似乎可以让它加载到SyndicationFeed中
System.Xml.XmlException: 'Element' is an invalid XmlNodeType. Line 282, position 4.
HttpWebRequest req = WebRequest.Create(@"http://www.facebook.com/feeds/page.php?id=202296766494181&format=atom10") as HttpWebRequest;
req.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
using (Stream responseStream = req.GetResponse().GetResponseStream())
{
using (XmlReader xr = XmlReader.Create(responseStream))
{
SyndicationFeed feed = SyndicationFeed.Load(xr);
}
}
public class FaceBookReader : XmlTextReader
{
public FaceBookReader(Stream stream)
: base(stream) { }
public FaceBookReader(String url)
: base(url) { }
public override void ReadEndElement()
{
string elementTag = this.LocalName.ToLower();
base.ReadEndElement();
// When we've read the channel End Tag, we're going to skip all tags
// until we reach the a new Ending Tag which should be that of rss
if (elementTag == "channel")
{
while (base.IsStartElement())
{
base.Skip();
}
}
}
}