Wriju's BLOG

.NET, Cloud and everything

Windows Phone 7 : Building Simple RSS Reader

Here we will build simple RSS reader in Windows Phone. Reading RSS is reading XML file online. So you need some engineering between XML and XAML. Here you go.

Your phone UI

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <ListBox Name="lstRSS">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Path=Title}"></TextBlock>
                    <TextBlock Text="{Binding Path=PubDate}"></TextBlock>
                    <TextBlock Text=" "></TextBlock>
                </StackPanel>                    
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>            
</Grid>

Then the class to define the structure

public class RSSClass
{
    public string Title { get; set; }
    public string PubDate { get; set; }
}

After that we need few lines to read it online.

public Page1()
{
    InitializeComponent();

    WebClient myRSS = new WebClient();
    myRSS.DownloadStringCompleted += new DownloadStringCompletedEventHandler(myRSS_DownloadStringCompleted);

    //Read Async
    myRSS.DownloadStringAsync(new Uri(@"http://blogs.msdn.com/b/wriju/rss.aspx"));
}

void myRSS_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    //Check if the Network is available
    if (!Microsoft.Phone.Net.NetworkInformation.DeviceNetworkInformation.IsNetworkAvailable)
    {
        var rssData = from rss in XElement.Parse(e.Result).Descendants("item")
                        select new RSSClass
                        {
                            Title = rss.Element("title").Value,
                            PubDate = rss.Element("pubDate").Value
                        };
        lstRSS.ItemsSource = rssData;
    }
    else
    {
        MessageBox.Show("No network is available..");
    }
}

Namoskar!!!