Build an RSS reader in 5 minutes

Today, I stumbled upon a very handy class : SyndicationFeed. This class, introduced in .NET 3.5, allows to manipulate syndication feeds (like RSS 2.0 or Atom 1.0) with very little code. It can be used to create and publish our own feeds, or to read existing ones.

For instance, here’s how to retrieve the news feed from Google News and display its title, its hyperlink, and the titles of it’s items :

string url = "http://news.google.fr/nwshp?hl=fr&tab=wn&output=rss";
using (XmlReader reader = XmlReader.Create(url))
{
    SyndicationFeed feed = SyndicationFeed.Load(reader);
    Console.WriteLine(feed.Title.Text);
    Console.WriteLine(feed.Links[0].Uri);
    foreach(SyndicationItem item in feed.Items)
    {
        Console.WriteLine(item.Title.Text);
    }
}

Easy enough, don’t you think ? 🙂

Let’s now take advantage of WPF binding capabilities to create a very simple graphical RSS reader :

<Window x:Class="TestFeeds.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Minimalist feed reader" Height="286" Width="531">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <DockPanel Grid.Row="0">
            <Button Name="btnGo"
                    DockPanel.Dock="Right"
                    Width="50"
                    Content="Go"
                    Click="btnGo_Click" />
            <TextBox Name="txtUrl" />
        </DockPanel>
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="250"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <ListBox Name="lstFeedItems"
                     Grid.Column="0"
                     DisplayMemberPath="Title.Text" />
            <GridSplitter Grid.Column="1"
                          VerticalAlignment="Stretch"
                          Width="3"
                          ResizeBehavior="PreviousAndNext"
                          ResizeDirection="Columns"/>
            <Frame Name="frmContents"
                   Source="{Binding SelectedItem.Links[0].Uri, ElementName=lstFeedItems}"
                   Grid.Column="2"
                   NavigationUIVisibility="Visible">
            </Frame>
        </Grid>
    </Grid>
</Window>

The code-behind :

    private void btnGo_Click(object sender, RoutedEventArgs e)
    {
        using (XmlReader reader = XmlReader.Create(txtUrl.Text))
        {
            SyndicationFeed feed = SyndicationFeed.Load(reader);
            lstFeedItems.ItemsSource = feed.Items;
        }
    }

And here’s the result !

Screenshot

10 thoughts on “Build an RSS reader in 5 minutes”

    1. Hi karan,
      So basically you want me to do your work for you, for free… sorry, but that’s not going to happen. Now, if you have a specific question regarding this article, I’ll be glad to answer it if I can.

  1. Hi,

    In your example, we get the whole webpage, but I would like to be able to get title, description etc … separately to avoid loading the whole page and format as I want. Do you have any idea of how to proceed ?

    Thanks.

    1. Hi,
      You can get the title and summary from the SyndicationItem, but depending on the feed, it might contain the full article or not… If you need the full article and the feed doesn’t contain it, you will need to scrape the website, and there’s no generic way of doing it since every site is different. You might also consider using a mobilizer like Instapaper or Readability.

  2. Hi,
    I have a similar rss reader code and in SyndicationFeed feed = SyndicationFeed.Load(reader); throws me a NotSupportedException. Do you have any idea why this happens and what to do to fix it? Sry for my English

    1. Hi,
      Sorry but I don’t have enough information to help you… you should post your question on Stackoverflow, where it’s easier to discuss your problem.

  3. which library does SyndicationFeed class refer. i tried using System.ServiceModel; its not working .

    1. It’s in the System.ServiceModel.Syndication namespace, in the System.ServiceModel assembly.

  4. Hi,

    I just tried this and wanted to say how great its is – good fun to play with and very useful – thanks for the article.

    Regards

    Eadmund

Leave a Reply

Your email address will not be published. Required fields are marked *