.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Read RSS feed in Silverlight

Posted By:Dhananjay Kumar       Posted Date: March 15, 2010    Points: 25    Category: Silverlight    URL: http://www.dotnetspark.com  

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

Reading RSS feed in Silverlight 3.0

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


namespace ReadingFeeds
{
  public class MyFeed
  {

  public string Title { get; set; }  
  public string Summary { get ; set ;}
  public string PublishedDate { get; set; }
  public Uri  Url { get; set; }

  }
}

Step 3

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.

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

MainPage.Xaml

  
  
    
  
  
  
  
   

  
  
  
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.

  WebClient proxy = new WebClient();
  proxy.OpenReadCompleted += new  OpenReadCompletedEventHandler(proxy_OpenReadCompleted);
  proxy.OpenReadAsync(serviceURI);


Where serviceURI is RSS URL; we are going to fetch.

2.  Create a list to read RSS feeds.

List<MyFeed> lstFeed = null;

3.  Add reference of

  using System.ServiceModel.Syndication;

4.  Read result return from service in a Stream.
5.  Create XMLReader from the stream.
6.  Load SyndicationFeed from the XMLReader.

  Stream stream = e.Result;
  XmlReader response = XmlReader.Create(stream);
  SyndicationFeed feeds = SyndicationFeed.Load(response);

 
7.  Fetch all the SyndicationItem ina foreach loop and create MyFeed instance and add to list.
8.  Bind list as datasourec of List box.

MainPage.Xaml.cs

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.IO;
using System.Xml;
using System.ServiceModel.Syndication;
using System.Collections; 

namespace ReadingFeeds
{
  public partial class MainPage : UserControl
  {

  List lstFeed = null;
  MyFeed feed = null;
  
  public MainPage()
  {
  InitializeComponent();
  btnFetchFeed.Click += new RoutedEventHandler(btnFetchFeed_Click);
  btnClear.Click += new RoutedEventHandler(btnClear_Click);  

  }

  void btnClear_Click(object sender, RoutedEventArgs e)
  {
  myListBox.SelectedIndex = -1;
  myListBox.ItemsSource = null; 
  txtRssFeed.Text = "";

  }

  void btnFetchFeed_Click(object sender, RoutedEventArgs e)
  {
  
  Uri serviceURI = null; 
  if (string.IsNullOrEmpty(txtRssFeed.Text))
  {
  MessageBox.Show("RSS Field is empty ");
   }
  else
  {
  serviceURI = new Uri(txtRssFeed.Text.Trim());
  WebClient proxy = new WebClient();
  proxy.OpenReadCompleted += new OpenReadCompletedEventHandler(proxy_OpenReadCompleted);
  proxy.OpenReadAsync(serviceURI);
  }  
  
  }

  void proxy_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
  {

  lstFeed = new List();
  Stream stream = e.Result;
  XmlReader response = XmlReader.Create(stream);
  SyndicationFeed feeds = SyndicationFeed.Load(response);  
  foreach (SyndicationItem f in feeds.Items)
  {  
  Uri imgUri = f.BaseUri;
  feed = new MyFeed() { Title = f.Title.Text, Summary = f.Summary.Text, PublishedDate = f.PublishDate.ToString(), Url = imgUri };
  lstFeed.Add(feed);
  }
  myListBox.ItemsSource = lstFeed; 
  }
  }
}

Press F5 and run the application.

Conclusion


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

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend