Pete
Pete

Reputation: 10871

Serializing a container of RSS feeds?

I am working on a project for an RSS Client. Right now I'm retrieving a feed using SyndicationFeed and an XmlReader and adding it to a list:

SyndicationFeed feed = SyndicationFeed.Load(
    XmlReader.Create("SOME URL TO A FEED"));
List<SyndicationFeed> feeds = new List<SyndicationFeed>();
feeds.Add(feed);

SyndicationFeed and most of its properties are not serializable. I need to be able to save the feeds and their respective items when my program is closed. I have a database solution working with Entity Framework but I would like to get away from this. So my next thought was to simply serialize the container with all the feeds but that's no go. Should I write serializabl class that mimics the SyndicationFeed and its properties and do a sort of boxing and unboxing or is there a better way?

Upvotes: 2

Views: 770

Answers (1)

casperOne
casperOne

Reputation: 74560

The SyndicationFeed class has a SaveAsAtom10 method and a SaveAsRss20 method, both of which take a XmlWriter instance which you can use anything as the underlying store for.

Personally, I'd go with the SaveAsAtom10 method, as I believe Atom is the richer format.

That said, you can easily persist this into a larger single document by creating a root element and child element in your own namespace, and then having the contents of each feed as the child, like so:

<feeds xmlns="http://tempuri.org/MyFeedContainer">
    <feed>
        <!-- Atom feed -->
    </feed>
    <feed>
        <!-- Rss feed -->
    </feed>
    <!-- And so on.. -->   
</feeds>

I'd use an XDocument and XElement instances to handle creating the container above, as namespace management is much easier when using those classes. Additionally, the XElement class exposes a CreateWriter and a CreateReader, which will expose XmlWriter and XmlReader instances respectively, which you can then pass to your SaveAsAtom/SaveAsRss20 methods.

However, I'd impress upon you to store each of the items separately; depending on how many feeds you have, creating one massive super document might be too much of a drain on resources, depending on your needs. Single document instances persisted in individual entities which you can access independently would probably be much more efficient to process.

You can still use the SaveAsAtom10 and a SaveAsRss20 methods to serialize the feeds.

Upvotes: 3

Related Questions