Friday, February 20, 2009

Silverlight Berita Harian

Hi dudes,

Today I managed to upload my first Silverlight application that read RSS feed data about articles and news. This application was developed using Microsoft Expression Blend 2 SP1 and Visual Studio 2008 SP1. Due to Silverlight security limitation to access cross domain server, I have to create a proxy server to read the rss feed and push it back to my silverlight application . You can see the live demo
here. The RSS feed of course taken from Berita Harian website (www.bharian.com.my). This is just a demo to showcase something what Silverlight can do.

Here is the screenshot :-















The animation takes place when you hover your mouse on the left menus.

Here is the code snippet to call the rss feed :-

void GetFeed()

{
WebClient client = new WebClient();

client.OpenReadCompleted += new OpenReadCompletedEventHandlerclient_OpenReadCompleted);
client.OpenReadAsync(new Uri(LINK));
}

It is recommended that you used OpenRead rather than DownloadString. This method is the Microsoft best practice on how to read xml from URI.

Here is the code snippet to process the result :-


void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error != null)
{
return;
}

try
{
using (Stream s = e.Result)
{
XDocument doc = XDocument.Load(s);
if (doc != null)
{
IEnumerable items = from rss in doc.Element("rss").Element("channel").Elements("item")
select rss;


if (items != null && items.Count() > 0)
{
spListBox.Children.Clear();
foreach (XElement item in items)
{
FeedData feeditem = new FeedData();
feeditem.txtFeedTitle.Text = item.Element("title").Value;
feeditem.txtSummary.Text = item.Element("description").Value;
feeditem.URL = item.Element("link").Value;
spListBox.Children.Add(feeditem);
}
}
}
}
}
catch (Exception ex)
{
//do something
}
}


Another technology I used to process the result is Linq to XML. You need to add the System.Xml.Linq assembly in your project in order to use this feature.

Currently this application have been published at
silverlight.net showcase. To be more specific, in the showcase page, click on the geography menu at the top left corner, then click on the Malaysia flag.

If you guys got any comments, please jot it down.


1 comment:

  1. awesome..
    u should try news dashboard kinda stuffss.. with more native language suppout (chinese or tamil newspapers).. sure big hit man!

    ReplyDelete