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(@"https://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!!!
Comments
Anonymous
November 15, 2012
Hi!!! I'm using this example but when I try to compile sends me a message that "XElement" doesn't exist ThanksAnonymous
December 14, 2012
The comment has been removedAnonymous
November 11, 2013
Error "Xelement", how can i solve this?Anonymous
November 12, 2013
@Simone - I think we need to have XElement (not Xelement)Anonymous
February 26, 2014
You need to add refernce for System.Xml.Linq to solve 'XElement' error.