Objective
This article is going to explain; how we can read RSS feeds in Silverlight 3.0.
Expected output
- User will enter RSS URL in text box.
- On click of Fetch Feed button ; RSS items will get populated
- 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
- Divide grid in three rows.
- In first and second row add stack panels.
- Give orientation of stack panel as horizontal.
- In first row; add one text block and one text box. User will enter RSS URL in text box provided here.
- In second row; add two buttons. One button to fetch URL and other to clear the values.
-
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}” />
|
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”>
|
Step 4
-
To read RSS feed; first we need to create instance of WebClient class. This class helps us to make HTTP call from Silverlight
-
Create a list to read RSS feeds.
-
Add reference of using System.ServiceModel.Syndication;
- Read result return from service in a Stream.
- Create XMLReader from the stream.
-
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.
Leave a Reply