Monday, February 11, 2013

Windows phone RSS FEED READER


This sample shows you how to create a basic RSS reader that downloads an RSS feed, and then displays the feed items in a ListBox.
This sample shows you how to create a basic RSS reader that downloads the http://www.nydailynews.com/gossip/gatecrasher/index_rss.xml RSS feed, and then displays the feed items in a ListBox. Although the sample is set up to download a specific RSS feed, you could choose to extend the sample to use in your app by adding functionality to dynamically download other RSS feeds.

Create project and have a MainPage.xaml layout as follows

<phone:PhoneApplicationPage 
    x:Class="Recipe1_RssReader.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="CH6-Recipes" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="RSS Feeds" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>
       

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBox Height="72" HorizontalAlignment="Left" Margin="0,52,0,0" Name="textBox1" Text="http://www.nydailynews.com/gossip/gatecrasher/index_rss.xml" VerticalAlignment="Top" Width="460" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="14,32,0,0" Name="textBlock1" Text="RSS Feed Link:" VerticalAlignment="Top" />
            <Button Content="Go" Height="72" HorizontalAlignment="Left" Margin="0,116,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
        </Grid>
        <Grid x:Name="ContentPanel2" Margin="12,217,12,-13" Grid.Row="1">
            <ListBox x:Name ="lstItems" Margin="0,6,0,0">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            
                            <StackPanel VerticalAlignment="Top">
                                <Image Width="200" Height="400" Source="{Binding Path=ImageURL}" />
                                <TextBlock Grid.Row="0" Text="{Binding Title}" FontWeight="Bold" Foreground="OrangeRed"/>
                                <TextBlock Grid.Row="1"  Text="{Binding Content}" />
                                <TextBlock Grid.Row="2"  Text="{Binding DatePosted}" />
                                <HyperlinkButton TargetName="_blank" Grid.Row="3"  Name="DetailLink" Content="Details..." NavigateUri="{Binding URL}" Foreground="Yellow" HorizontalAlignment="Left"/>
                                
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>


    </Grid>

</phone:PhoneApplicationPage>


the MainPage.xaml.cs code is given below

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 Microsoft.Phone.Controls;

// reference System.Xml.Linq
using System.Xml.Linq;
namespace Recipe1_RssReader
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        WebClient remoteXml;
        public MainPage()
        {
            InitializeComponent();

            remoteXml = new WebClient();
            remoteXml.DownloadStringCompleted += new DownloadStringCompletedEventHandler(remoteXml_DownloadStringCompleted);
        }

        //private void remoteXml_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        //{
        //    if (e.Error != null)
        //        return;
        //    XDocument xdoc = XDocument.Parse(e.Result);
        //    IEnumerable<RssFeed> feeds;
        //   feeds = from task in xdoc.Descendants("item")
        //            select new RssFeed
        //            {
        //                Title = task.Attribute("title").Value,
        //                //Content = task.Attribute("description").Value,
        //                DatePosted = DateTime.Parse(task.Attribute("pubDate").Value),
        //                URI = task.Attribute("link").Value
        //            };
        //    lstTasks.ItemsSource = feeds;
        //}

        private void remoteXml_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                return;

            XDocument xdoc = XDocument.Parse(e.Result);
            var rss = XElement.Parse(e.Result);

            XNamespace ns = rss.GetNamespaceOfPrefix("media");
            List<RssFeed> rssFeeds;

            rssFeeds = (from item in xdoc.Descendants("channel").Elements("item")
                        select new RssFeed()
                        {
                            Title = item.Element("title").Value,
                            Content = item.Element("description").Value,
                            DatePosted = item.Element("pubDate").Value, 
                            URL = new Uri(item.Element("link").Value, UriKind.Absolute),
                            ImageURL = item.Element(ns + "content").Attribute("url").Value

                        }).ToList();
            lstItems.ItemsSource = rssFeeds;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string txtUri = textBox1.Text;
            txtUri = Uri.EscapeUriString(txtUri);
            Uri uri = new Uri(txtUri,UriKind.Absolute);
            remoteXml.DownloadStringAsync(uri);
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            
             
           // App.currentURL = new Uri
            NavigationService.Navigate(new Uri("/BrowserPage.xaml", UriKind.Relative));
        }
    }
}

RssFeed.cs is having following getter and setters

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Recipe1_RssReader
{
        public class RssFeed
        {
            public string Title { get; set; }
            public string Content { get; set; }
            public String DatePosted { get; set; }
            public Uri URL { get; set; }
            public String ImageURL { get; set; }
            
        }
   
}

after running the project successfully you can see the output as follows


Download the code from the link RSSFEED 



No comments:

Post a Comment