Pull RSS feeds from a Facebook page

I need help pulling RSS feeds from a facebook page. I use the following code, but it continues to give me an error:

string url = "https://www.facebook.com/feeds/page.php?id=40796308305&format=rss20"; XmlReaderSettings settings = new XmlReaderSettings { XmlResolver = null, DtdProcessing=DtdProcessing.Parse, }; XmlReader reader = XmlReader.Create(url,settings); SyndicationFeed feed = SyndicationFeed.Load(reader); foreach (var item in feed.Items) { Console.WriteLine(item.Id); Console.WriteLine(item.Title.Text); Console.WriteLine(item.Summary.Text); } if (reader != null) reader.Close(); 

This code works fine with any blog or rss page, but using Facebook rss it throws an exception with the following post

An element named 'html' and namespace 'http://www.w3.org/1999/xhtml' is not a valid feed format.

thanks

+6
source share
3 answers

Facebook will return the HTML in this instance because it does not like the user agent provided by XmlReader. Since you cannot configure it, you will need another solution to capture the feed. This should solve your problem:

 var req = (HttpWebRequest)WebRequest.Create(url); req.Method = "GET"; req.UserAgent = "Fiddler"; var rep = req.GetResponse(); var reader = XmlReader.Create(rep.GetResponseStream()); SyndicationFeed feed = SyndicationFeed.Load(reader); 

This is strictly Facebook behavior, but the proposed change should work equally well for other sites that are ok with your current implementation.

+10
source

It works when using the Gregorys code above if you change the feed format to atom10 instead of rss20. Change URL:

 string url = "https://www.facebook.com/feeds/page.php?id=40796308305&format=atom10"; 
+2
source

In my case, it was also very difficult to use the Facebook feed, and then I try to burn the feed for my facebook page with feedburner. Feedburner generated a feed for me in Atom1.0 format. And then I successfully :) consumed this with the system.syndication class, which was my code:

 string Main() { var url = "http://feeds.feedburner.com/Per.........all"; Atom10FeedFormatter formatter = new Atom10FeedFormatter(); using (XmlReader reader = XmlReader.Create(url)) { formatter.ReadFrom(reader); } var s = ""; foreach (SyndicationItem item in formatter.Feed.Items) { s+=String.Format("[{0}][{1}] {2}", item.PublishDate, item.Title.Text, ((TextSyndicationContent)item.Content).Text); } return s; } 
+1
source

Source: https://habr.com/ru/post/890154/


All Articles