Objective

This article is going to explain; how we can read RSS feeds in Silverlight 3.0.

Expected output

  1. User will enter RSS URL in text box.
  2. On click of Fetch Feed button ; RSS items will get populated
  3. On Clear Search button click text box and list box will be cleared.


So, let us start follow the below steps

Step 1

Create a Silverlight application.

Step 2

Add an entity class. In this class RSS Feeds would be read. Right click Silverlight project and add a class. Give any name; I am giving name here MYFeed.

MyFeed.cs

    1  namespace ReadingFeeds

    2     {

    3         public class MyFeed

    4         {

    5 

    6             public string Title { get; set; }

    7             public string Summary { get; set; }

    8             public string PublishedDate { get; set; }

    9             public Uri Url { get; set; }

   10 

   11         }

   12     }

   13 

 

Design the page

  1. Divide grid in three rows.
  2. In first and second row add stack panels.
  3. Give orientation of stack panel as horizontal.
  4. In first row; add one text block and one text box. User will enter RSS URL in text box provided here.
  5. In second row; add two buttons. One button to fetch URL and other to clear the values.
  6. In third row put a list box. Return items of RSS will be bind to this list box. List box is bind as follows.

     

<ListBox x:Name=”myListBox” Grid.Row=”2″ ScrollViewer.HorizontalScrollBarVisibility=”Disabled” >

<ListBox.ItemTemplate>

<DataTemplate>

<StackPanel Orientation=”Vertical” >

<TextBlock x:Name=”txtTitle” FontSize=”14″
Text=”{Binding Title}”/>

<TextBlock x:Name=”txtSummary” Text=”{Binding Summary}”
TextWrapping=”Wrap”/>

<TextBlock x:Name=”txtPublishedDate”
Text=”{Binding PublishedDate}” />


<TextBlock x:Name=”txtUri” Text=”{Binding Url}” />


<Rectangle Height=”3″ Fill=”Blue” />


</StackPanel>


</DataTemplate>


</ListBox.ItemTemplate>


</ListBox>

 There is stack panel inside data template. There are different text blocks bind with different data.
MainPage.Xaml

<Grid x:Name=”LayoutRoot”>

<Grid.RowDefinitions>

<RowDefinition Height=”1*” />

<RowDefinition Height=”1*” />

<RowDefinition Height=”12*” />

</Grid.RowDefinitions>

<StackPanel Grid.Row=”0″ Orientation=”Horizontal” HorizontalAlignment=”Center”>


<TextBlock x:Name=”lblFeed” Height=”40″
Text=”RSS FEED” FontSize=”18″
/>


<TextBox x:Name=”txtRssFeed” Height=”40″ Width=”1000″ Background=”AliceBlue” />


</StackPanel>


<StackPanel Grid.Row=”1″ Orientation=”Horizontal”>


<Button x:Name=”btnFetchFeed” Height=”30″ Width=”100″ Content=”Fetch Feed” VerticalAlignment=”Center” />


<Button x:Name=”btnClear” Height=”30″ Width=”100″ Content=”Clear Search” VerticalAlignment=”Center” />


</StackPanel>


<Rectangle Height=”5″ Fill=”Blue” Grid.Row=”0″ VerticalAlignment=”Bottom” />


<Rectangle Height=”5″ Fill=”Blue” Grid.Row=”1″ VerticalAlignment=”Bottom” />


<ListBox x:Name=”myListBox” Grid.Row=”2″ ScrollViewer.HorizontalScrollBarVisibility=”Disabled” >


<ListBox.ItemTemplate>


<DataTemplate>


<StackPanel Orientation=”Vertical” >


<TextBlock x:Name=”txtTitle” FontSize=”14″
Text=”{Binding Title}”/>


<TextBlock x:Name=”txtSummary” Text=”{Binding Summary}”
TextWrapping=”Wrap”/>


<TextBlock x:Name=”txtPublishedDate”
Text=”{Binding PublishedDate}” />


<TextBlock x:Name=”txtUri” Text=”{Binding Url}” />


<Rectangle Height=”3″ Fill=”Blue” />


</StackPanel>


</DataTemplate>


</ListBox.ItemTemplate>


</ListBox>


</Grid>

Step 4

  1. To read RSS feed; first we need to create instance of WebClient class. This class helps us to make HTTP call from Silverlight 
  1. Create a list to read RSS feeds. 
  2. Add reference of     using System.ServiceModel.Syndication;
  3. Read result return from service in a Stream.
  4. Create XMLReader from the stream.
  5. Load SyndicationFeed from the XMLReader. Fetch all the SyndicationItem ina foreach loop and create MyFeed instance and add to list. Bind list as datasourec of List box.
     MainPage.Xaml.cs
      

        1 

        2 using System;

        3 using System.Collections.Generic;

        4 using System.Linq;

        5 using System.Net;

        6 using System.Windows;

        7 using System.Windows.Controls;

        8 using System.Windows.Documents;

        9 using System.Windows.Input;

       10 using System.Windows.Media;

       11 using System.Windows.Media.Animation;

       12 using System.Windows.Shapes;

       13 using System.IO;

       14 using System.Xml;

       15 using System.ServiceModel.Syndication;

       16 using System.Collections;

       17 

       18 namespace ReadingFeeds

       19 {

       20     public partial class MainPage : UserControl

       21     {

       22 

       23         List<MyFeed> lstFeed = null;

       24         MyFeed feed = null;

       25 

       26         public MainPage()

       27         {

       28             InitializeComponent();

       29             btnFetchFeed.Click += new RoutedEventHandler(btnFetchFeed_Click);

       30             btnClear.Click += new RoutedEventHandler(btnClear_Click);

       31 

       32         }

       33 

       34         void btnClear_Click(object sender, RoutedEventArgs e)

       35         {

       36             myListBox.SelectedIndex = –1;

       37             myListBox.ItemsSource = null;

       38             txtRssFeed.Text = “”;

       39 

       40         }

       41 

       42         void btnFetchFeed_Click(object sender, RoutedEventArgs e)

       43         {

       44 

       45             Uri serviceURI = null;

       46             if (string.IsNullOrEmpty(txtRssFeed.Text))

       47             {

       48                 MessageBox.Show(“RSS Field is empty “);

       49             }

       50             else

       51             {

       52                 serviceURI = new Uri(txtRssFeed.Text.Trim());

       53                 WebClient proxy = new WebClient();

       54                 proxy.OpenReadCompleted += new OpenReadCompletedEventHandler(proxy_OpenReadCompleted);

       55                 proxy.OpenReadAsync(serviceURI);

       56             }

       57 

       58         }

       59 

       60         void proxy_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)

       61         {

       62 

       63             lstFeed = new List<MyFeed>();

       64             Stream stream = e.Result;

       65             XmlReader response = XmlReader.Create(stream);

       66             SyndicationFeed feeds = SyndicationFeed.Load(response);

       67             foreach (SyndicationItem f in feeds.Items)

       68             {

       69                 Uri imgUri = f.BaseUri;

       70                 feed = new MyFeed() { Title = f.Title.Text, Summary = f.Summary.Text, PublishedDate = f.PublishDate.ToString(), Url = imgUri };

       71                 lstFeed.Add(feed);

       72             }

       73             myListBox.ItemsSource = lstFeed;

       74         }

       75     }

       76 }

       77 

       78  

    Press F5 and run the application.

    Conclusion

    In this article I discussed how to read RSS in Silverlight 3.0. Thanks for reading.

Advertisements

4 thoughts on “Reading RSS feed in Silverlight 3.0

  1. wow, great, I was wondering how to cure acne naturally. and found your site by google, learned a lot, now i’m a bit clear. I’ve bookmark your site and also add rss. keep us updated.

  2. Hi! Dear Dhananjay
    After I Created a Silverlight application, I couldn’t add the reference of using System.ServiceModel.Syndication at MainPage.Xaml.cs.
    How could I solve the problem ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s