WPF RSS Reader

RSS stands for "Real Simple Syndication" and is used for publishing updates of frequently updated content. It is a concept that allows syndication of content between two places.

RSS is used in a significantly large amount of industries from banks to publishers, businesses to news, and many more. RSS files are XML based files that contain formatted plain text.The beauty of RSS is that it can be used for any type of content. Simple alerts , podcasts and videocasts are few examples. .

I was looking to build a light version myself and Windows Presentation Foundation (WPF) was the primary candidate for the technology.

Let's dive in.

There is a rich XmlDataProvider class and a lighter but effective SyndicationFeed for you to start with. There are many examples on XMLDataProvider on the web; hence, I wanted to try the SyndicationFeed to create a quick RSS reader. WPF lets you create one with ease within minutes.

The following items would be used in this sample.

The Webclient can be used to download the contents of a specified url. You can even utilize the asynchronous implementation of the downloadstring method provided by this class to avoid freezing your User interface components.

webClientInstance.DownloadStringAsync(new Uri(theURLString));

Once the webclient downloads the data, it returns it as a string. We can use a stringreader to read the result. The result is obtained from the DownloadStringAsync method call of the Webclient instance.

StringReader stringReaderInstance = new StringReader(e.Result);

SyndicationFeed is a .Net inbuilt syndication class that can be used to work with feeds. It has a set of helpful properties that help you read the feed object effectively. Description, Authors, Copyright, Items, Title are few helpful properties. The following code snippet shows you how to read a webclient's response to a syndication feed.

XmlReader reader                = XmlReader.Create(new StringReader(e.Result));
SyndicationFeed downloadFeed    = SyndicationFeed.Load(reader);

Every feed also provides some interesting stuff like Image of the RSS feed provider. This can also be retrieved using the syndication feed. The following code snippet shows you how to do that.

<Image x:Name="feedImage" Grid.Column="0" Stretch="Fill"></Image>
SyndicationFeed downloadFeed = SyndicationFeed.Load(reader);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(imageUrl, UriKind.RelativeOrAbsolute);
bi.EndInit();
feedImage.Source = bi;

The listbox is bound to the Summary, Title, and PublishDate objects of the Syndication Feed Item. Following is the markup of the listbox.

<ListBox Name="listBoxFeedItems" IsSynchronizedWithCurrentItem="True"
        ScrollViewer.CanContentScroll="True">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <Expander ExpandDirection="Right">
                                <TextBlock Text="{Binding Path=Summary.Text}" 
                                    TextTrimming="WordEllipsis" />
                            </Expander>
                                    <TextBlock Text="{Binding Path=Title.Text}"
                                    ToolTip="{Binding Path=PublishDate.Date}"/>
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
</ListBox>

Incidentally the more feeds I tried with SyndicationFeed, the more I found a new kind of problem. All feeds do not follow the standard specifications. One such non-standard item is the DateTime fields in the feed item. So you may end up writing your own custom XML Reader that derives and enhances Xml Text Reader.

I found an interesting post at the Microsoft connect, which solves this issue and is the way to go.

I hope you enjoyed the write-up. Thanks for reading!

References:

XMLDataProvider

Syndication feed load fails to Parse datetime



About the Author

Srinath M S

I would love to leave a footprint in this flat world

Related Articles

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

  • On-demand Event Event Date: March 27, 2014 Teams need to deliver quality software faster and need integrated agile planning, task tracking, source control, auto deploy with continuous builds and a configurable process to adapt to the way you work. Rational Team Concert and DevOps Services (JazzHub) have everything you need to build great software, integrated seamlessly together right out of the box or available immediately in the cloud. And with the Rational Team Concert Client, you can connect your …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds