Posted by admin
September - 16 - 2009

RSS is an essential part of the internet and is almost used on every website from youtube to Silverlight Forums. It’s no surprise that one of the most common questions I am asked is how do you read RSS in Silverlight? Well the answer is pretty straight forward and I am going to show you how to do it.

For this tutorial I am going to consume an RSS feed from Silverlight Forums.

First create you project and once it’s setup then right click on ‘References‘ in the Silverlight application as shown

Silverlight Add Reference

Silverlight Add Reference

Select ‘Add Reference‘ and choose ‘System.ServiceModel.Syndication

Silverlight Add Reference - Synidication

Silverlight Add Reference - Syndication

Now that the project is setup you can now get on with the coding :) On the Xaml I have added a ScrollViewer and a StackPanel. On the ScrollViewer I have set ‘ScrollViewer.VerticalScrollBarVisibility=”Auto”‘ to auto so that a vertical scrollbar will be visible.

<UserControl x:Class="SilverlightForums_ReadRSS.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
  <StackPanel x:Name="LayoutRoot" Background="AliceBlue">
      <HyperlinkButton Content="Silverlight Forums" NavigateUri="http://www.silverlightforums.com" TargetName="_blank" FontSize="16" FontWeight="Bold" Foreground="Black" HorizontalAlignment="Right" Margin="50, 50, 50, 0" />
      <ScrollViewer Height="100" Margin="50, 0, 50, 50" Background="White" ScrollViewer.VerticalScrollBarVisibility="Auto">
            <StackPanel x:Name="LinkList" Height="Auto" Width="Auto" />
      </ScrollViewer>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <TextBlock Text="Created by" />
            <HyperlinkButton Content="Silverlight Tutorials" NavigateUri="http://www.silverlighttutorials.com" TargetName="_blank" Foreground="Black" />
            <TextBlock Text="and" />
            <HyperlinkButton Content="Silverlight Forums" NavigateUri="http://www.silverlightforums.com" TargetName="_blank" Foreground="Black" />
            <TextBlock Text="." />
      </StackPanel>
    </StackPanel>
</UserControl>

On the mainpage.xaml.cs I have setup the following private members

        #region Private Members
        private Uri RSSUrl;
        private XmlReader reader;
        private SyndicationFeed feed;
        #endregion

Then I set the RSS Url to my Uri and then called a method called GetRSSFeed()

        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            RSSUrl = new Uri("http://www.silverlightforums.com/external.php?type=RSS2");
            GetRSSFeed();
        }

The GetRSSFeed() method creates and instance of WebClient which is used to retrieve data and is best used for a one-time retrieval of data (which is exactly what we are doing!). Once our WebClient is created we can then register the ‘DownloadStringCompleted’ event so once our WebClient has completed retriving the data it will trigger that event. To Kick the retrieval of the RSS feed off I call the WebClient.DownloadStringAsync(Uri) helper method.

        private void GetRSSFeed()
        {
            WebClient client = new WebClient();
            client.DownloadStringAsync(RSSUrl);
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
        }

Once the download is complete the following is triggered.

        private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            reader = XmlReader.Create(new StringReader(e.Result));
            feed = SyndicationFeed.Load(reader);

            foreach (SyndicationItem item in feed.Items)
            {
                HyperlinkButton link = new HyperlinkButton();
                link.Content = item.Title.Text;
                link.NavigateUri = item.Links.First().Uri;
                link.TargetName = "_blank";
                LinkList.Children.Add(link);
            }
        }
e.Result fill with RSS data

e.Result full with RSS data

Now this code processes the RSS feed. An XmlReader is setup and is passed the data from the WebClient (e.Result is the RSS feed :) ). The reader reads all the data in the StringReader and then the SyndicationFeed (feed) is setup which in turn loads the data from the XmlReader (reader).

SyndicationItem populated with RSS node

SyndicationItem populated with RSS node

Once that is done you can then loop through your RSS nodes and I have done this with a foreach loop. For each SyndicationItem inside the feed.Items collection it will then create a HyperLinkButton with the feed items title and link Uri set to the HyperLinkButton. Finally the HyperLinkButton control is added to my StackPanel which is called LinkList.

Build the project and run the application and you should see this

Silverlight 3 Reading RSS

Silverlight 3 Reading RSS

Here you can see the RSS feed displayed as HyperLinkButtons in a ScrollViewer. To update the feed simply refresh the page in the browser.

Download the example and take a look. If you have any issues or questions feel free to discuss them at Silverlight Forums.

Comments are closed.

About Silverlight Forums

Silverlight Forums Stats

Silverlight Forums is an established Silverlight community for people interested in design and development using Silverlight.

11045 news articles
29 tutorials and 2 video tutorials
2060 forum posts in 1298 threads

Silverlight User Groups (SLUGs)

Why not join your local SLUG?

London, Atlanta, Phoenix, Toronto, Portland, Wellington, Los Angeles, Seattle, South Florida, Tampa Bay, Jacksonville, Belgium

Silverlight Usergroups (SLUGs)