Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

The haunting of SyndicationFeed

DZone's Guide to

The haunting of SyndicationFeed

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

So let’s talk about reading RSS feeds in a .NET application. When someone mentions RSS or Atom, I automatically think of SyndicationFeed as the cornerstone for the solution. Indeed, that’s an acceptable object for most instances. Or is it?

I recently got an email from a person who was testing my RSS reader for Windows Phone 7 (you can read more about it here and here). It worked fine for most feeds, but not for this one:

http://feeds.feedburner.com/LeadStoriesFromAol

I’m pretty sure that this is not the only feed that might cause problems, but it was included in the report so I decided to test against it. When the user supplied this feed for syndication, the application crashed, throwing an error:

image

The problem lies deep in an incorrectly formatted date present in a feed item (or multiple items for that matter). Interestingly enough, this was not the case for a lot of other feeds that were tested by me when I released the project. The person who wrote me the email referenced this question on StackOverflow – someone already reported the same problem. Obviously, it was also reported on Microsoft Connect. However, it is marked as Closed (Fixed).

image

But it’s not even close to being actually fixed – I created a sample .NET 4.0 project and the problem persists. Maybe a different side of the problem was fixed, but an incorrect date passed inside the feed still causes a crash.

Generally, in my RSS reader I would use a WebClient to download the feed string and then parse it via SyndicationFeed. I would trigger the DownloadStringCompleted event and use the following code:

XmlReader reader = XmlReader.Create(new StringReader(e.Result));
SyndicationFeed feed = SyndicationFeed.Load(reader);
foreach (SyndicationItem sItem in feed.Items)
{
if ((sItem != null) && (sItem.Summary != null) && (sItem.Title != null))
{
App.Model.FeedItems.Add(
new ViewModel.ItemModel()
{
ItemDetails = sItem.Summary.Text,
ItemTitle = sItem.Title.Text,
ItemLink = sItem.Links[0].Uri.ToString()
}
);
}
} 

But since an error is imminent for invalid threads, I decided to eliminate it and use XDocument instead. Via a simple LINQ query I extract the needed data and pass it to my ViewModel.

XDocument document = XDocument.Parse(e.Result);
var x = from c in document.Root.Element("channel").Elements("item") select c;
foreach (XElement unit in x)
{
App.Model.FeedItems.Add(
new ViewModel.ItemModel()
{
ItemDetails = unit.Element("description").Value,
ItemTitle = unit.Element("title").Value,
ItemLink = unit.Element("link").Value
}
);
} 

So if you are parsing feeds via SyndicationFeed, I would recommend you having this backup method in case the SyndicationFeed instance you are using fails to parse the feed correctly.

I updated my WP7 feed reader project and you can download the new source here. I tried to highlight the parts of code that were modified to avoid the error.

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}