Working with RSS or Atom Feeds

Microsoft Silverlight will reach end of support after October 2021. Learn more.

HTTP-based Web services frequently use syndication feeds to return data back to the client. Silverlight version 4 supports the RSS 2.0 and Atom 1.0 syndication feed formats.

Assume that after making a request to the HTTP-based Web service as described in How to: Make Requests to HTTP-Based Services, the syndication feed content is returned inside a responseStream object of type Stream.

To parse the feed, get an XmlReader over the stream and load it into a SyndicationFeed class.

XmlReader responseReader = XmlReader.Create(responseStream);
SyndicationFeed feed = SyndicationFeed.Load(responseReader);

The items of the feed are now accessible through the Items collection of objects of type SyndicationItem. The collection can be accessed directly or through data binding, as shown in the following two sections.

Accessing Feed Items Directly

Feed items can be accessed directly by iterating through the Items collection, as shown in the following code.

foreach (SyndicationItem item in feed.Items)
{
    string title = item.Title.Text;
    DateTimeOffset date = item.PublishDate;

    // Access other elements as necessary

}

Accessing Feed Items Through Data Binding

We use XAML to declare a UI element to display the feed items.

<ListBox x:Name="itemsList" ItemsSource="{Binding}" />

The DataContext property is set in code-behind to the collection of feed items to establish the data binding relationship.

itemsList.DataContext = feed.Items;

We define a template to map some properties of the SyndicationItem type to UI elements. We render the list of feed items in UI using hyperlinks.

<ListBox x:Name="itemsList" ItemsSource="{Binding}">
   <ListBox.ItemTemplate>
      <DataTemplate>
         <HyperlinkButton Content="{Binding Title.Text}}" 
                          NavigateUri="{Binding Links, Converter={StaticResource linkFormatter}}" />
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

Note that the Links property is a collection and it cannot be data bound directly to the NavigateUri property, which is of type Uri. An I:System.Windows.Data.IValueConverter is defined to do the necessary transformation.

public class LinkFormatter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return ((Collection<SyndicationLink>)value).FirstOrDefault().Uri;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

The LinkFormatter class just defined is instantiated as a XAML resource as shown below.

<UserControl x:Class="SyndicationFeedReader.Page"
             xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:SyndicationFeedReader"
             >
   <UserControl.Resources>
      <local:LinkFormatter x:Key="linkFormatter"/>
   </UserControl.Resources>

   ...

Size Considerations when Using Syndication

When you use the SyndicationFeed class, references to the System.Runtime.Serialization.dll and System.Xml.Serialization.dll assemblies are added to the Silverlight project. This increases the size of the .xap file for the compiled Silverlight project. The System.Xml.Serialization.dll assembly is not required in all cases, and can safely be removed if the Silverlight project does not use its functionality. The following syndication types indicate a dependency on System.Xml.Serialization.dll: XmlSyndicationContent and SyndicationElementExtension.

To remove the assembly, first change the extension of the compiled Silverlight control from .xap to .zip. Then double-click the renamed file to open it. Locate and delete the System.Xml.Serialization.dll assembly. Ensure that the Silverlight control is still functional after the change.