[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.

Run View
Language:

Prerequisites (available from the Silverlight download site):

  • Microsoft Silverlight 2.0 Beta 2.

  • Microsoft Visual Studio 2008.

  • Microsoft Silverlight Tools Beta 2 for Visual Studio 2008.

  • Microsoft Silverlight SDK 2.0 Beta 2.

For more resources on Web service support in Silverlight, please see Silverlight Web Services Samples.

To create the Silverlight client application

  1. Open Microsoft Visual Studio 2008.

  2. From the File menu, select New, then Project, then Silverlight, and then select Silverlight Application template.

  3. Name the Project BasicSyndication and click OK.

  4. In the Add Silverlight Application window that pops up, select Dynamically Generate an HTML test Silverlight within this project.

  5. Click OK.

To add references to the assemblies required by the application

  1. Add a reference to System.ServiceModel.Syndication.dll to the BasicSyndication project:

    1. In the Solution Explorer, right-click the References folder under the project folder and choose Add Reference.

    2. In the .NET tab in the Add Reference dialog select System.ServiceModel.Syndication and click OK.

  2. Add the following using statements to the top of the Page.xaml.cs file.

    using System.ServiceModel.Syndication;
    using System.Xml;

To create an HTTP request to the syndication feed

  1. 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.

  2. 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"));
  3. Make an asynchronous HTTP request and register a callback.

    request.BeginGetResponse(new AsyncCallback(responseHandler), request);
  4. 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

  1. Get an HTTP response in the responseHandler callback function.

    void responseHandler(IAsyncResult asyncResult)
    {
      HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
      HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
    }
  2. 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

  1. Add a TextBlock control to Page.xaml, by adding the following line inside the Canvas control.

    <TextBlock x:Name="feedContent" /> 
  2. 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;
     }
  3. 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;
          }
    }
  4. 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.