Academic Students Projects | Software School Projects | Free Source Codes | College
Projects By LANGUAGE
Libraries
Articles & seminars
Source Code

The price of the projects include source code, abstract, report and support for development and deployment. Please use our contact us form or send email to Support@srishtis.com

 

 

 

Reading and Displaying RSS Feed in a Silverlight DataGrid
The SyndicationFeed class in Silverlight 2 makes it easy to parse the feed response. The process of reading a feed is as simple as reading a RSS/ATOM feed using the WebClient class, load the stream into the SyndicationFeed class and bind it to a Silverlight UI. Let us see how to read and display feed in a Silverlight control:
Step 1: Open Visual Studio 2008 > File > New Project > Select the language (C# or VB.NET) > Select ‘Silverlight’ in the Project Types > from the templates, select ‘Silverlight Application’.
Step 2: Type a name (ConsumingRSS) and location for the project> click OK.
Choose the first option ‘Add a new ASP.NET Web project to the solution to host Silverlight’ and the project type as ‘ASP.NET Web Site’ and click OK. You will see that two projects are created: ConsumingRSS.Web and ConsumingRSS.
Step 3: Right click ConsumingRSS project> Add Reference > .NET Tab > Add a reference to System.ServiceModel.Syndication. In the Page.xaml.cs or vb add the following namespaces:
Now drag and drop a TextBox, a Button and a DataGrid control from the toolbox to the Page.xaml. After rearranging these controls in the Grid, the markup will look similar to the following:
<UserControl xmlns:data="clr-namespace:System.Windows.Controls;
assembly=System.Windows.Controls.Data"  x:Class="ConsumingRSS.Page"
    xmlns="http://schemas.microsoft.com/winfx/
2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBox x:Name="txtFeedLoc" Width="450" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" ></TextBox>
<Button x:Name="btnFetch" Grid.Row="0" Content="Fetch RSS" Width="100" HorizontalAlignment="Left" Grid.Column="1"  Click="btnFetch_Click"></Button> <data:DataGrid x:Name="dGrid" IsReadOnly="True" CanUserSortColumns="True"
Grid.Row="1" Grid.ColumnSpan="2" AutoGenerateColumns="False" >
</data:DataGrid>
</Grid>
</UserControl>
If you observe, a click handler for the Button has been created. On the click event, we read the Feed URI from the textbox. The entire code is listed below:
using System;
using System.Xml;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.ServiceModel.Syndication;
using System.IO;
using System.Collections.Generic;
using System.Linq;
namespace ConsumingRSS
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
           
        }
 
        protected void LoadRSS(string uri)
        {
            WebClient wc = new WebClient();           
            wc.OpenReadCompleted +=new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
            Uri feedUri = new Uri(uri, UriKind.Absolute);
            wc.OpenReadAsync(feedUri);
        }
 
     private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                txtFeedLoc.Text = "Error in Reading Feed. Try Again later!!";
                return;
            }
            using (Stream s = e.Result)
            {
                SyndicationFeed feed;
                List<SyndicationItem> feedItems = new List<SyndicationItem>();
 
                using (XmlReader reader = XmlReader.Create(s))
                {
                    feed = SyndicationFeed.Load(reader);
                    foreach (SyndicationItem feedItem in feed.Items)
                    {
                        feedItems.Add(feedItem);
                    }
                    var posts = from item in feedItems
                                select new RSSFeed()
                                {
                                    Title = item.Title.Text,
                                    NavURL = item.Links[0].Uri.AbsoluteUri,
                                    Description = item.Summary.Text
                                };
                    dGrid.ItemsSource = posts;                   
                    dGrid.Visibility = Visibility.Visible;
                }               
            }
        }
 
        private void btnFetch_Click(object sender, RoutedEventArgs e)
        {
            if(txtFeedLoc.Text != String.Empty)
                LoadRSS(txtFeedLoc.Text.Trim());
        }
    }
 
    public class RSSFeed
    {
        public string Title { get; set; }
        public string NavURL { get; set; }
        public string Description { get; set; }
    }
}
The mark up is shown below:
<data:DataGrid x:Name="dGrid" AutoGenerateColumns="False" IsReadOnly="True" CanUserSortColumns="True" Grid.Row="1" Grid.ColumnSpan="2">
            <data:DataGrid.Columns>
                <data:DataGridTemplateColumn>
                    <data:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <HyperlinkButton Content="{Binding Title}" NavigateUri="{Binding NavURL}" TargetName="_blank"/>
                                <local:HtmlTextBlock Text="{Binding Description}" TextWrapping="Wrap" UseLayoutRounding="True"/>
                            </StackPanel>
                        </DataTemplate>
                    </data:DataGridTemplateColumn.CellTemplate>
                </data:DataGridTemplateColumn>
            </data:DataGrid.Columns>
</data:DataGrid>