[This topic is pre-release documentation and is subject to change in future releases of Microsoft Silverlight.]
Accessing Syndication Feeds with Silverlight
Introduction
This topic discusses how to create a Silverlight client that can access a syndication service exposing either an RSS 2.0 or Atom 1.0 syndication feed.
Prerequisites (available from the Silverlight download site):
-
Microsoft Silverlight 2.0 Beta 1.
-
Microsoft Visual Studio 2008.
-
Microsoft Silverlight Tools Beta 1 for Visual Studio 2008.
-
Microsoft Silverlight SDK 2.0 Beta 1.
For more resources on Web service support in Silverlight, please see Silverlight Web Services Samples.
To create the Silverlight client application
-
Open Microsoft Visual Studio 2008.
-
From the File menu, select New, then Project, then Silverlight, and then select Silverlight Project template.
-
Name the Project BasicSyndication and click OK.
-
In the Add Silverlight Application window that pops up, select Generate an HTML test Silverlight within this project.
To add references to the assemblies required by the application
-
Add a reference to System.ServiceModel.Syndication.dll to the BasicSyndication project:
-
In the Solution Explorer, right-click the References folder under the project folder and choose Add Reference.
-
In the .NET tab in the Add Reference dialog select System.ServiceModel.Syndication and click OK.
-
Repeat this step to add a reference to the System.Net.dll to the project.
-
Add the following statements to the top of the Page.xaml.cs file.
using System.ServiceModel.Syndication;
using System.Net;
using System.Xml;
To create an HTTP request to the syndication feed
-
Identify the syndication feed you want to access in this control and note its address. In this example we use http://silverlight.net/blogs/microsoft/rss.aspx. Note that the server hosting the feed must opt-in to cross-domain access by providing a Clientaccesspolicy.xml or Crossdomain.xml file at the root of the domain.
-
Create an HTTP request to the syndication feed within the Page() implementation in the Page.xaml.cs file. Use the feed address you noted in the previous step.
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://silverlight.net/blogs/microsoft/rss.aspx"));
-
Make an asynchronous HTTP request and register a callback.
request.BeginGetResponse(new AsyncCallback(responseHandler), request);
-
The implementation of the Page method should now contain the following code.
public Page()
{
// Required to initialize variables
InitializeComponent();
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://silverlight.net/blogs/microsoft/rss.aspx"));
request.BeginGetResponse(new AsyncCallback(responseHandler), request);
}
To get an XMLReader to the stream and instantiate the SyndicationFeed class
-
Get an HTTP response in the responseHandler callback function.
void responseHandler(IAsyncResult asyncResult)
{
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
}
-
Inside the implementation of the responseHandler, get an XML reader to the stream and load it into the SyndicationFeed class.
XmlReader reader = XmlReader.Create(response.GetResponseStream());
SyndicationFeed feed = SyndicationFeed.Load(reader);
To use the SyndicationFeed
-
Add a TextBlock control to Page.xaml, by adding the following line inside the Canvas control.
<TextBlock x:Name="feedContent" />
-
Once the SyndicationFeed object is generated, iterate through the Items collection to access individual SyndicationItem objects. This example adds the title and date of each item to a TextBox control.
foreach (SyndicationItem item in feed.Items)
{
feedContent.Text += "* " + item.Title.Text + Environment.NewLine;
feedContent.Text += " Published on: " + item.PublishDate + Environment.NewLine;
}
-
The asynchronous request callback implementation should now contain the following code.
void responseHandler(IAsyncResult asyncResult)
{
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
XmlReader reader = XmlReader.Create(response.GetResponseStream());
SyndicationFeed feed = SyndicationFeed.Load(reader);
foreach (SyndicationItem item in feed.Items)
{
feedContent.Text += "* " + item.Title.Text + Environment.NewLine;
feedContent.Text += " Published on: " + item.PublishDate + Environment.NewLine;
}
}
-
You are now ready to view your control. Press Ctrl + F5 and Visual Studio displays the automatically generated BasicSyndicationTestPage.aspx. You should be able to see a list of the items in your feed in the browser window.