SilverBullet, Silverlight

SilverBullet #11 – SyndicationFeed

silverbulletI’d like to provide you with a SilverBullet™, a small snippet of Silverlight, a class or namespace hidden in the silverlight .NET framework, to help you out in times of need. It’s not to learn, but something to keep in your pocket. Just remember it’s there and you’re safe.

Occasionally it’s required to use an Rss or an Atom feed in your projects. Just to show some news, or the latest update on the stock exchange. Working with these, often large, chucks of xml directly can be a pain. In Silverlight 3 (and .NET 3.5 and 4)  the System.ServiceModel.Syndication.SyndicationFeed class has been added. This class makes the processing of the feed a lot easier.

Say you have an service getting an Atom feed from somewhere and returning this as a string (how to do this can be found in an earlier tutorial I wrote about .Net Ria Services and Rss). Basically you’ll end up with a large string containing xml which represents an Atom or Rss feed. For the creation of a new SyndicationFeed you can use its static Load() method. This method takes an XmlReader object as a parameter, which is created using a StringReader, which takes a string. Got that? Here’s the code to clarify:

using(var reader = XmlReader.Create(new StringReader(AnXmlString)))
{
  var feed = SyndicationFeed.Load(reader);  
  // do something useful with the feed
}

 

To use this feed in your xaml, you can data-bind to properties like Title, Description and Copyright. The SyndicationFeed class also has a property which gets or sets a list of Items, a list of Authors and a list of Categories.

A lot of these properties use types which can be found in System.ServiceModel.Syndication also, to provide an easy use of the building block of the feeds, Classes like SyndicationItem, SyndicationPerson and TextSyndicationContent.

Another thing that’s worth mentioning, the SyndicationFeed class can also be used the other way around, to create feeds. After filling all the necessary properties, you can call the SaveAsAtom10 or SaveAsRss20 methods to write the output using a XmlWriter.

 

Last, have a look here for bit of source code to play with.

 

Shout it
Tags van Technorati: Silverlight
dotNed blogger
kick it on DotNetKicks.com

Leave a Reply

Seo wordpress plugin by www.seowizard.org.
%d bloggers like this: