I am currently developing a windows 8 metro application using C# and XAML. I am reading RSS feeds from several sources using the Syndication method and it is working perfectly but the RSS from FACEBOOK is not actually and is crashing with the error "INVALID XML". Does reading it as XmlDocument work? How to do it?
Below is my code:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using Windows.Data.Xml.Dom;
using Windows.Web.Syndication;
namespace App1
{
public class FeedData
{
public string title { get; set; }
public string description { get; set; }
public DateTime pubDate { get; set; }
private List<FeedItem> _Items = new List<FeedItem>();
public List<FeedItem> Items
{
get
{
return this._Items;
}
}
}
// FeedItem
// Holds info for a single blog post
public class FeedItem
{
public string title { get; set; }
public string description { get; set; }
public DateTime pubDate { get; set; }
public Uri link { get; set; }
}
// FeedDataSource
// Holds a collection of blog feeds (FeedData), and contains methods needed to
// retreive the feeds.
public class FeedDataSource
{
private ObservableCollection<FeedData> _Feeds = new ObservableCollection<FeedData>();
public ObservableCollection<FeedData> Feeds
{
get
{
return this._Feeds;
}
}
public async Task GetFeedsAsync(Int32 ID)
{
if (ID == 1003)
{
Task<FeedData> feed1 =
GetFeedAsync("http://outbound.indevcogroup.com/feeds/posts/default/-/INDEVCO%20Group");
this.Feeds.Add(await feed1);
}
else if (ID == 1002)
{
Task<FeedData> feed12 =
GetFeedAsync("http://www.facebook.com/feeds/page.php?format=rss20&id=126332847400326");
this.Feeds.Add(await feed12);
}
}
private async Task<FeedData> GetFeedAsync(string feedUriString)
{
// using Windows.Web.Syndication;
SyndicationClient client = new SyndicationClient();
Uri feedUri = new Uri(feedUriString);
try
{
SyndicationFeed feed = await client.RetrieveFeedAsync(feedUri);
// This code is executed after RetrieveFeedAsync returns the SyndicationFeed.
// Process it and copy the data we want into our FeedData and FeedItem classes.
FeedData feedData = new FeedData();
feedData.title = feed.Title.Text;
if (feed.Subtitle != null && feed.Subtitle.Text != null)
{
feedData.description = feed.Subtitle.Text;
}
// Use the date of the latest post as the last updated date.
feedData.pubDate = feed.Items[0].PublishedDate.DateTime;
foreach (SyndicationItem item in feed.Items)
{
FeedItem feedItem = new FeedItem();
feedItem.title = item.Title.Text;
feedItem.pubDate = item.PublishedDate.DateTime;
// Handle the differences between RSS and Atom feeds.
if (feed.SourceFormat == SyndicationFormat.Atom10)
{
feedItem.description = item.Content.Text;
feedItem.link = new Uri("http://www.scoop.it/t/agricultural-horticultural- news" + item.Source);
}
else if (feed.SourceFormat == SyndicationFormat.Rss20)
{
feedItem.description = item.Summary.Text;
feedItem.link = item.Links[0].Uri;
}
feedData.Items.Add(feedItem);
}
return feedData;
}
catch (Exception)
{
return null;
}
}
}
}
Like this:
// this example pulls coca-cola's posts
var _Uri = new Uri("http://www.facebook.com/feeds/page.php?format=rss20&id=40796308305");
// including user agent, otherwise FB rejects the request
var _Client = new HttpClient();
_Client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
// fetch as string to avoid error
var _Response = await _Client.GetAsync(_Uri);
var _String = await _Response.Content.ReadAsStringAsync();
// convert to xml (will validate, too)
var _XmlDocument = new Windows.Data.Xml.Dom.XmlDocument();
_XmlDocument.LoadXml(_String);
// manually fill feed from xml
var _Feed = new Windows.Web.Syndication.SyndicationFeed();
_Feed.LoadFromXml(_XmlDocument);
// continue as usual...
foreach (var item in _Feed.Items)
{
// do something
}
Best of luck!