Silverlight Tips provides simple and useful tutorials and tips with real life examples, live demos and sample codes to download.
About authors:
Damon Serji
Damon Serji,
Silverlight developer working at Metia in London.
Gavin Wignall
Gavin Wignall,
Interactive Design Lead, working in design for over 10 years, the last 3 being in Silverlight.
Allan Muller
Allan Muller,
Developer, working on various types of Silverlight and WCF projects.
Get Microsoft Silverlight Latest posts from SilverlightTips.com

Displaying a RSS feed is a very straight forward task in Silverlight.

In summary: we need to access and download the RSS (XML file) and read it as a single string value, read the downloaded string as XML using XmlReader class, load the XML into a variable of type SyndicationFeed, and finally using a loop extract each node and save it in a desirable variable.

Some of the Silverlight built-in classes used in this example are:

Uri: Takes the URL of the RSS feed
XmlReader: Represents a reader that provides fast, non-cached, forward-only access to XML data” (msdn)
SyndicationFeed: Represents a top-level feed object, <feed> in Atom 1.0 and <rss> in RSS 2.0” (msdn)
WebClient: Receives data from a recource defuned by Uri (in our case a URL to RSS or XML file)

I am going to demonstrate how to read and display a RSS feed (XML file) in three simple steps:

Step 1: Create a custom RSS class
here I am just creating a new public class (RSSItem) for my RSS feed to contain any information I would like to keep for each item in the RSS. This will help to keep my project tidy and my code meaningful. In my RSSItem class I will add as many properties as I need:

public class RSSItem
{
    #region Properties
    public string RSSTitle {get; set;}
    public string RSSDescription { get; set; }
    public Uri RSSLink { get; set; }
    #endregion
}

Step 2: Reading the RSS feed in Silverlight
create three private variables for Uri, XmlReader and SyndicationFeed (for this you need to add a new reference in your project to System.ServiceModel.Syndication – do this by right clicking on ‘References’ folder in you Silverlight project and clicking on ‘Add Reference...’)

private Uri rssUri;
private XmlReader xmlReader;
private SyndicationFeed feed;

and don't forget to add the namespaces:

using System.Xml;
using System.ServiceModel.Syndication;

Now, in your constructor or ‘Loaded’ method add the following:

rssUri = new Uri("http://feeds.feedburner.com/Silverlighttips");
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(rssUri);

WebClient is another built-in class in Silverlight and is used to open the XML (RSS). Since the operation is done asynchronously the callback method (DownloadStringCompleted) is called after the operation is done.

Step 3: Populate RSS nodes
first, add an ItemsControl in the MainPage.xaml:


<ItemsControl x:Name="RSS" ItemsSource="{Binding}" >
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <HyperlinkButton x:Name="Title" NavigateUri="{Binding RSSLink}"
             Width="305" Margin="0,22,0,15" Foreground="#FF10589F"
             FontSize="12" FontWeight="Bold">
                <HyperlinkButton.Content>
                  <TextBlock Text="{Binding RSSTitle}" TextWrapping="Wrap" />
               </HyperlinkButton.Content>
            </HyperlinkButton>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

In the ItemsControl we can bind the required properties from our RSSItem class using {Binding nameOfProperty}.

Now, from our DownloadStringCompleted method in MainPage.xaml.cs we can retrieve the RSS in form of a string. Then, we can create our XmlReader and load it to a SyndicationFeed variable which treats the XML as a 'RSS' and allow us to use its properties:

private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error == null)
    {
        int itemsCount = 5;
        xmlReader = XmlReader.Create(new StringReader(e.Result));
        feed = SyndicationFeed.Load(xmlReader);
       
        List<RSSItem> itemsList = new List<RSSItem>();

        if (feed.Items.Count() < 5)
        {
            itemsCount = feed.Items.Count();
        }

        for (int i = 0; i <= itemsCount; i++)
        {
            RSSItem rssitem = new RSSItem();
            rssitem.RSSTitle = feed.Items.ToList()[i].Title.Text;
            rssitem.RSSLink = feed.Items.ToList()[i].Links[0].Uri;
            itemsList.Add(rssitem);
        }
        RSS.ItemsSource = itemsList;
    }
}

you will need to add the namespace for the StringReader:

using System.IO;

In the above method from top:

  • check if there are any errors in the result, if not continue
  • set the number of items to read
  • load the XML into a SyndicationFeed variable
  • create a new List of type RSSItem to hold each RSSItem
  • if the number of total RSS items to be found (itemsCount) is greater than the total number of RSS items available then go with the total number of available items
  • search in each node for ‘Title’ and ‘Summary’ (description)
  • add Title and Summary of each RSS item to the variable of type RSSItem class, and finally add the rssitem to the RSSItem list
  • at the end we bind our itemsList to 'RSS' which is an ItemsControl in our XAML page
Posted by Damon Serji on 29. November 2009 14:52 under: Intermediate
 with 6 Comments

Comments

Damon:

There is a common error you might get during making this app, and that is security error :

"TargetInvocationException was unhandled by user code"

Basically this happens because you are doing cross domain while the site you are getting the feed from doesnt allow access to you.

To solve this issue follow the instruction here: silverlightforums.com/showpost.php

Posted 11/29/2009 2:50:12 PM

Damon - there is a bug in your code. I have posted the fix here www.silverlightforums.com/showthread.php

Posted 12/9/2009 5:01:17 PM

Thanks Allan,

The actuall code was fine on CodePlex I just missed that on the post due to change of idea during typing! I updated the post now..

Posted 12/11/2009 7:36:41 AM

@allan : thx for your info dude, The actuall code was fine in your post

Posted 12/22/2009 7:44:26 AM

Thanks for sharing the code. I think I'll start wiuth it my next weekend project Wink. Up to now my experience with Silverlight is quite low...

Posted 1/13/2010 5:40:05 AM

chandra mohan:

It is showing RSSItem does not contain a definition of ItemsSource,

how to solve this?
thanks....

Posted 4/14/2010 7:48:53 AM

Add comment


(Will show your Gravatar icon)

biuquote
  • Comment
  • Preview