In this recipe, we will learn how to consume RSS feeds. RSS (Really Simple Syndication) feeds are published by many news agencies and informational websites such as CNN. RSS/ATOM is also used extensively in publishing blogs. RSS is based on XML format.
Let's try to check what is published at a CNN feed first. There are many feeds available at CNN. We will use one feed that returns the Top Stories. Check the following URL: http://rss.cnn.com/rss/cnn_topstories.rss. You should get content similar to that shown in the following screenshot, which contains the name of the top story with a link, posted date, and short description:
The following is the partial XML feed:
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?> <?xml-stylesheet type="text/css" media="screen" href="http://rss.cnn.com/~d/styles/itemcontent.css"?> <rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"> <channel> <title>CNN.com</title> <link>http://www.cnn.com/?eref=rss_topstories</link> <description>CNN.com delivers up-to-the-minute news and information on the latest top stories, weather, entertainment, politics and more.</description> <language>en-us</language> <copyright>� 2011 Cable News Network LP, LLLP.</copyright> <pubDate>Sun, 24 Jul 2011 23:16:13 EDT</pubDate> <ttl>5</ttl> <image> <title>CNN.com</title> <link>http://www.cnn.com/?eref=rss_topstories</link> <url>http://i2.cdn.turner.com/cnn/.element/img/1.0/logo/ cnn.logo.rss.gif</url> <width>144</width> <height>33</height> <description>CNN.com delivers up-to-the-minute news and information on the latest top stories, weather, entertainment, politics and more.</description> </image> ... ...
In this recipe, we will create an application that will have a textbox to collect any RSS feed link and display the updates from the feed provider. Also, when you click the item in the list box you will be directed to the site to display the details.
Recipe1_RssReader
. RssFeed.cs
. Open the file and add a new class RssFeed
to collect the feed data:public class RssFeed { public string Title { get; set; } public string Content { get; set; } public DateTime DatePosted { get; set; } public Uri URL { get; set; } }
MainPage.xaml
file and add three rows with two TextBlock
controls and a HyperlinkButton:
<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"> <TextBlock Grid.Row="0" Text="{Binding Title}" FontWeight="Bold" Foreground="OrangeRed"/> <TextBlock Grid.Row="1" Text="{Binding Content}" /> <HyperlinkButton TargetName="_blank" Grid.Row="2" Name="DetailLink" Content="Details..." NavigateUri="{Binding URL}" Foreground="Yellow" HorizontalAlignment="Left"/> </StackPanel> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid>
MainPage.xaml.cs
file. As we are going to use LINQ to XML, let's add a reference to the assembly System.Xml.Linq
to the project, and then add the using
declarative at the beginning of the file:using System.Xml.Linq;
WebClient
object and the DownloadStringCompleted
event handler as shown in the following partial code snippet:public partial class MainPage : PhoneApplicationPage { // Constructor WebClient remoteXml; public MainPage() { InitializeComponent(); remoteXml = new WebClient(); remoteXml.DownloadStringCompleted += new DownloadStringCompletedEventHandler (remoteXml_DownloadStringCompleted); } …
DownloadStringCompleted
event method. In this method, we download the XML file into the XDocument
object and then using LINQ to XML we parse the three elements we need to save:private void remoteXml_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { if (e.Error != null) return; XDocument xdoc = XDocument.Parse(e.Result); List<RssFeed> rssFeeds; rssFeeds = (from item in xdoc.Descendants("item") select new RssFeed() { Title = item.Element("title").Value, Content = item.Element("description").Value, URL = new Uri(item.Element("link").Value, UriKind.Absolute), }).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); }
For navigating to RSS feeds, we basically have two choices in Phone 7: one is by using the WebClient
and the other is HttpWebRequest
. Here we use the WebClient
.
In the DownloadStringCompleted
method, we use XDocument
to parse the remote RSS feed XML file. Once it is downloaded, we parse the XML using a LINQ to XML query. Based on the feed XML, we just collected three elements: title, description, and link.
For more information on the WebClient
class check this MSDN site: http://msdn.microsoft.com/en-us/library/system.net.webclient(v=VS.95).aspx.