How to Ingest an RSS Feed in Silverlight

Introduction

In this article we will learn how to consume an RSS feed in Silverlight. Additionally we will also look into fetching the results and binding them onto the Silverlight DataGrid. Sample source codes have also been included in this article for demonstration purpose.

For the sample application I have used Visual Studio 2010 and Silverlight 4.0.

Importance of RSS Feeds

In recent days most of the popular websites, for example even the Microsoft websites, provided all the updates and new information through publishing RSS feeds. Hence in most applications that are developed, the ingestion of the RSS feed has become almost inevitable. This will facilitate the client application to process and display the feed content.

So I thought it would be informative to write an article on ingesting an RSS feed in a Silverlight application.

Supporting Classes in Silverlight

Silverlight is packed with some supporting classes for the RSS feed ingestion and processing. The namespace to include for making use of these classes is System.ServiceModel.Syndication. Some of the classes are listed below.

  • SysdicationContent
  • SyndicationFeed
  • SyndicationItem, etc.

The RSS feed response from the given RSS Uri needs to be fetched using the WebClient class and these syndication classes are for easy parsing of the RSS feed content.

Sample Code

Create a sample Silverlight application and name it as RSSFeedIngestionDemo. In the MainPage.xaml define a textbox, a button and a Silverlight DataGrid. So the idea for the sample application is to provide the RSS feed url in the textbox and clicking on the button would fetch the response, parse it and bind the content onto the DataGrid.

Below is the XAML code for MainPage.xaml.

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:DesignHeight="422" d:DesignWidth="581" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

Below is the code behind implementation in MainPage.xaml.cs file.

namespace RSSFeedIngestionDemo
{
public partial class MainPage : UserControl
{
WebClient _webClient = null;
public MainPage()
{
InitializeComponent();
_webClient = new WebClient();
_webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
}
private void GetResponseButton_Click(object sender, RoutedEventArgs e)
{
_webClient.OpenReadAsync(new Uri(UriTextBox.Text, UriKind.Absolute));
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
SyndicationFeed syndicationFeed = SyndicationFeed.Load(XmlReader.Create(e.Result));
var feeds = from feedItem in syndicationFeed.Items
select new
{
Title = feedItem.Title,
Content = feedItem.Summary.Text,
NavigateUri = feedItem.Links.FirstOrDefault().BaseUri.AbsoluteUri,
PublishedDate = feedItem.PublishDate,
LastUpdated = feedItem.LastUpdatedTime
};
RssResultsGrid.ItemsSource = feeds;
}
else
MessageBox.Show(string.Format("RSS feed ingestion failed. Failure Message: {0}", e.Error.Message));
}
}
}

In the above code, the WebClient class is used to fetch the XML response from the given RSS feed Uri. Then the response XML is loaded into the SyndicationFeed object where the SyndicationFeed.Load(xmlResponse) takes care of parsing the XML data and populating the SyndicationFeed object.

Now go ahead and run the sample application. Input a valid RSS Uri and click on the Get Response button. The result is bound to the Silverlight DataGrid.

Conclusion

Hope this article provides a clear overview of ingesting an RSS service in Silverlight. Please make use of the comments section to provide your comments.



About the Author

V.N.S Arun

I work for an MNC in Bangalore, India. I am fond of writing articles, posting answers in forums and submitting tips in dotnet. To contact me please feel free to make use of the "Send Email" option next to the display name.

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

  • "Security" is the number one issue holding business leaders back from the cloud. But does the reality match the perception? Keeping data close to home, on premises, makes business and IT leaders feel inherently more secure. But the truth is, cloud solutions can offer companies real, tangible security advantages. Before you assume that on-site is the only way to keep data safe, it's worth taking a comprehensive approach to evaluating risks. Doing so can lead to big benefits.

  • If you need new tools and tricks to make your meetings profitable and productive, then 5 Tips in 5 Minutes: A Quick Guide for More Profitable Sales Meetings is for you. Timely, practical tips that you can incorporate in just seconds will save you literally hours in travel and meeting time, not to mention help you to focus on what your sales prospects really want to know and how you can meet their needs. Get 5in5: A Quick Guide for More Profitable Sales Meetings and start building your sales the smarter, faster …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds