Syndication RSS Reader Fails Due To Invalid XML?

I wrote a code snippet that uses the System.ServiceModel.Syndication library to parse RSS feeds.

The problem is that for one of my channels (which is provided by facebook) I get the following line at the end of the answer, and the Syndication library does not parse the feed, because it says the text is invalid XML, and it says that this is because of this part :

  ... </channel> <access:restriction relationship="deny" xmlns:access="http://www.bloglines.com/about/specs/fac-1.0" /> </rss> 

Iโ€™m sure that something is missing here, because both the feed and the parsing library are from huge companies (Facebook and Microsoft, respectively).

Can any of you help? Or, alternatively, a better parser that does not rely on XML validity?

PS Here is my RSS feed URL:
http://www.facebook.com/feeds/page.php?id=202296766494181&format=rss20

This is how I parse the response to the feed:

 var stringReader = new StringReader(resp); var xreader = XmlReader.Create(stringReader); var xfeed = System.ServiceModel.Syndication.SyndicationFeed.Load(xreader); 

and the exception that I get:

 System.Xml.XmlException: 'Element' is an invalid XmlNodeType. Line 282, position 4. 

in System.Xml.XmlReader.ReadEndElement () ...

+4
source share
1 answer

SyndicationFeed seems to be having a problem with the access restriction element: facebook. See a recent topic at http://social.msdn.microsoft.com/Forums/ar/xmlandnetfx/thread/7045dc1c-1bd9-409a-9568-543e74f4578d

Michael Sun (MSFT) wrote: โ€œI just saw Martinโ€™s message! Very useful! I also did some research on this issue. An item from Bloglines, http://www.bloglines.com/index.html . It looks like the facebook extension uses to of their RSS 2.0 feeds, http://www.feedforall.com/access-namespace.htm . From this article it seems that Rss20FeedFormatter is not the only one that does not support elements.

I agree with Martin to use XDocument (LINQ to XML) to parse the RSS feed. Or, if you are creating some great application through C #, the CK SDK for Facebook can help, http://facebooksdk.codeplex.com/ "

Edit:

However, Atomfeed does not seem to suffer from this problem. So a simple solution would be to use this link ( http://www.facebook.com/feeds/page.php?id=202296766494181&format=atom10 ). Thus, changing the format parameter from rss20 to atom10

  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); } } 

Another alternative is to write a legacy XMLTextReader that overrides the ReadEndElement method, skipping any element after the channel close tag. (Do not forget that the code below is without warranty, as I consider myself to be a novice C # developer. Do not hesitate to fix any possible errors)

 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(); } } } } 
+8
source

All Articles