Accessing RSS Services : HttpWebRequest « Communication « Silverlight






Accessing RSS Services

    


<UserControl x:Class='SilverlightApplication3.MainPage'
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    xmlns:d='http://schemas.microsoft.com/expression/blend/2008' 
    xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' 
    mc:Ignorable='d' 
    d:DesignWidth='640' 
    d:DesignHeight='480'>
     <Grid x:Name="LayoutRoot" Background="LightBlue">
         <TextBlock Text="RSS Feed Items"/>
         <TextBlock Text="Item Summary"/>
         <ScrollViewer Background="White">
                <ListBox x:Name="titleList"/>
         </ScrollViewer>
         <ScrollViewer Background="White">
            <TextBlock x:Name="feedSummary"/>
         </ScrollViewer>
         <Button x:Name="getBtn" Content="Get Feed"/>
        <TextBlock x:Name="dateText" Text="Published: "/>
    </Grid>
</UserControl>
//File:Page.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;

using System.ServiceModel.Syndication;
using System.Net;
using System.Xml;

namespace SilverlightApplication3
{
    public partial class MainPage : UserControl
    {
        SyndicationFeed rssFeed;

        public MainPage()
        {
            InitializeComponent();

            getBtn.Click += new RoutedEventHandler(doGetFeed);

            titleList.SelectionChanged += new SelectionChangedEventHandler(doShowSummary);
         }

        void doShowSummary (object sender, SelectionChangedEventArgs e)
         {
            SyndicationItem item = rssFeed.Items.ElementAt(titleList.SelectedIndex);
             dateText.Text = "Published: "+ item.PublishDate.Date.ToString();
            feedSummary.Text = item.Summary.Text.ToString();
         }

        void doGetFeed(object sender, RoutedEventArgs e)
        {
            string feedURL = "http://localhost/MainFeed.aspx";
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(feedURL));
            request.BeginGetResponse(new AsyncCallback(responseHandler), request);
         }

        void responseHandler(IAsyncResult asyncResult){
            HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);

            XmlReader reader = XmlReader.Create(response.GetResponseStream());
            rssFeed = SyndicationFeed.Load(reader);

            foreach (SyndicationItem item in rssFeed.Items)
            {
                titleList.Items.Add(item.Title.Text);
            }
         }
    }
}

   
    
    
    
  








Related examples in the same category

1.Using the HttpWebRequest and HttpWebResponse ObjectsUsing the HttpWebRequest and HttpWebResponse Objects