Pulled Apart - Part XII: Parsing feeds (ATOM & RSS) in .NET
[
]
Note: This is part of a series—you can find the rest of the parts in the series index.
I’ve mentioned that a podcatcher is really just two things put together: a download manager and a feed parser. Feed parsing is not the easiest item to build—just look at my attempt many years ago to build a Delphi RSS parser called SimpleRSS. It works well, but there are many edge cases that can break it.
The key things that trip you up when writing a parser are:
- RSS and ATOM – There are two major formats for feeds, RSS and ATOM, which are very different.
- Versioning – RSS and ATOM both have a number of versions, each requiring completely different parsing logic.
- Errors – It’s easy to produce them; since feeds are just XML, there’s a lot of invalid feeds out there.
With that in mind, I’m really happy that the .NET Framework (since 3.5) includes its own parser for feeds: SyndicationFeed.
SyndicationFeed
System.ServiceModel.SyndicationFeed supports both ATOM (version 1.0) and RSS (version 2.0). To use it, add a reference to System.ServiceModel.dll. It only handles parsing (and feed creation, though I don’t need that functionality in Pull). To parse a feed, pass an XmlReader to the Load method, and it takes care of the rest.
using (XmlReader reader = XmlReader.Create(podcastUrl))
{
return SyndicationFeed.Load(reader);
}
That’s as simple as it gets! 😊