Written by:
Microsoft
Microsoft
Syndication Feeds
Mar 05, 2009
Login to Rate
:( Error
0
0
Summary
Describes how to create a Silverlight client that can access and display a syndication service.
This QuickStart describes how to create a Silverlight client
that can access and display a syndication service
that exposes either an RSS 2.0 or Atom 1.0 syndication feed.
This QuickStart contains the following sections:
Note
Syndication in Silverlight is based largely on the Windows Communication Foundation (WCF) object model. For an in-depth description of the WCF syndication feature set, see WCF Syndication in the MSDN Library.
Accessing a Syndication Feed with a Silverlight Client
Syndication items can be accessed in Silverlight by using a System.Net.WebClient object to asynchronously download and read strings using a System.Xml.XmlReader from a feed. The following example accesses a syndication feed that provides the top stories at Wired magazine. To try this example, click Click Here for the Feed. A list of feed items appears below the button.
The following managed code in the Page.xaml (Page.xaml.cs or Page.xaml.vb) file shows how you access the syndication items from the feed. Uri identifies the address of the feed to be accessed. It points to the Wired magazine syndication feed.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel.Syndication;
using System.Xml;
using System.IO;
namespace SyndicationFeedReader
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
void OnClick(object sender, EventArgs args)
{
WebClient client = new WebClient();
client.DownloadStringAsync(
new Uri("http://feeds.wired.com/wired/index?format=xml"));
client.DownloadStringCompleted +=
new DownloadStringCompletedEventHandler(request_DownloadStringCompleted);
}
void request_DownloadStringCompleted(object sender,
DownloadStringCompletedEventArgs e)
{
XmlReader reader = XmlReader.Create(new StringReader(e.Result));
SyndicationFeed feed = SyndicationFeed.Load(reader);
lbSyndicationItems.ItemsSource = feed.Items;
}
}
}
Visual Basic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes
Imports System.ServiceModel.Syndication
Imports System.Xml
Imports System.IO
Namespace SyndicationFeedReader
Public Partial Class Page
Inherits UserControl
Public Sub New()
InitializeComponent()
End Sub
Private Sub OnClick(sender As Object, args As EventArgs)
Dim client As New WebClient()
client.DownloadStringAsync(
New Uri("http://feeds.wired.com/wired/index?format=xml"))
client.DownloadStringCompleted +=
New DownloadStringCompletedEventHandler(request_DownloadStringCompleted)
End Sub
Private Sub request_DownloadStringCompleted(sender As Object,
e As DownloadStringCompletedEventArgs)
Dim reader As XmlReader = XmlReader.Create(New StringReader(e.Result))
Dim feed As SyndicationFeed = SyndicationFeed.Load(reader)
lbSyndicationItems.ItemsSource = feed.Items
End Sub
End Class
End Namespace
Displaying a Syndication Feed
Feed tiles can be displayed by binding the titles of the feed topics to the Text property of the TextBlock.
XAML
<UserControl x:Class="SyndicationFeedReader.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="600" Height="500">
<Grid x:Name="LayoutRoot" Background="Blue">
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition />
</Grid.RowDefinitions>
<Button
Grid.Row="0"
Margin=" 5"
Height="60"
Content="Click Here for the Feed"
Click="OnClick"/>
<ListBox x:Name="lbSyndicationItems" Margin="4" Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title.Text}"/>
</DataTemplate>
</ListBox.ItemTemplate>>
</ListBox>
</Grid>
</UserControl>
The following code shows how to bind the data from the SyndicationFeed object.
C#
lbSyndicationItems.ItemsSource = feed.Items;
Visual Basic
lbSyndicationItems.ItemsSource = feed.Items
Cross-Domain Calls
It is important to note that the call to the Wired site is a cross-domain call.
It works here because the Wired feed has a cross-domain file that enables it.
The cross-domain file can be accessed at Wired cross-domain file.
Other feeds will have to be similarly enabled for cross-domain calls if they are to be accessed in this way. For more information, see Making a Service Available Across Domain Boundaries in the Silverlight documentation
on MSDN.
See Also
Send Feedback