Tuesday, February 24, 2009

How to get data from msn navigation feed rss using .net

I have a project to develop a website for MSN and I have to read the feed (Microsoft own schema). Here is the code to save ur time :)



Let say i want to get a feed from this link

http://sg.msn.com/rss/navfeedrssensg.aspx



By using few lines of codes to get the feed:

MSNNavFeedMenu feedmenu = new
MSNNavFeedMenu("http://sg.msn.com/rss/navfeedrssensg.aspx");
string feedLinkMenu =
feedmenu.CreateNavLinks(feedmenu.GetNavLinks(4), 5);
string feedToolLinkMenu =
feedmenu.CreateNavToolLinks(feedmenu.GetNavToolLinks(), 5);


Here is the class:


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Web.Security;
using System.Net;
using System.IO;
using System.Xml;
using System.Collections.Generic;


public class NavFeedRss
{

private string _Title;
private string _Link;
private string _Icon;
private string _Description;

public string Description
{
get
{
return _Description;
}
}

public string Title
{
get
{
return _Title;
}
}

public string Link
{
get
{
return _Link;
}
}

public string Icon
{
get
{
return _Icon;
}
}


public NavFeedRss(string title, string description, string link, string icon)
{
_Title = title;
_Link = link;
_Icon = icon;
_Title = title;
_Description = description;
}

}

/// <summary>
/// Summary description for MSNNavFeedMenu
/// </summary>
public class MSNNavFeedMenu
{
private string rssUrl;


public MSNNavFeedMenu(string rsslink)
{
rssUrl = rsslink;
}


public string CreateNavLinks(List<NavFeedRss> RssNavMenu, int columns)
{
string itemTitles = String.Empty;

if (RssNavMenu.Count > 0)
{
//Create list


//Create dynamic row with specified fixed column
int row = Convert.ToInt32(Math.Floor(Convert.ToDouble(RssNavMenu.Count / columns)));
int reminder = RssNavMenu.Count % columns;

int c = 0;
int temp = 0;
for (int r = 1; r <= row; r++)
{
itemTitles += "<ul class='linklist1'>";
for (c = temp; c < (columns * r); c++)
{
if (r == 1)
{
itemTitles += "<li class='first'>";
}
else
{
itemTitles += "<li>";
}

itemTitles += "<a href='" + RssNavMenu[c].Link + "'>" + RssNavMenu[c].Title + "</a>";
itemTitles += "</li>";

}
temp = columns * r;
itemTitles += "</ul>";
}


//Add reminder item
itemTitles += "<ul class='linklist1'>";

for (c = temp; c < temp + reminder; c++)
{
if (c == temp)
{
itemTitles += "<li class='first'>";
}
else
{
itemTitles += "<li>";
}

itemTitles += "<a href='" + RssNavMenu[c].Link + "'>" + RssNavMenu[c].Title + "</a>";
itemTitles += "</li>";
}
itemTitles += "</ul>";


//Closing list


}



return itemTitles;
}


public string CreateNavToolLinks(List<NavFeedRss> RssNavMenu, int columns)
{

string itemTitles = String.Empty;

if (RssNavMenu.Count > 0)
{

//Create dynamic row with specified fixed column
int row = Convert.ToInt32(Math.Floor(Convert.ToDouble(RssNavMenu.Count / columns)));
int reminder = RssNavMenu.Count % columns;

int c = 0;
int temp = 0;
for (int r = 1; r <= row; r++)
{
itemTitles += "<ul class='linkedimglinklist1 cf'>";
for (c = temp; c < (columns * r); c++)
{
if (r == 1)
{
itemTitles += "<li class='first'>";
}
else
{
itemTitles += "<li>";
}



itemTitles += "<a href='" + RssNavMenu[c].Link + "'><img src='" + RssNavMenu[c].Icon + "' width='25' height='20' alt='Windows Live Messenger' /><span><strong>" + RssNavMenu[c].Title + "</strong></span></a>";
itemTitles += "</li>";

}
temp = columns * r;
itemTitles += "</ul>";
}


//Add reminder item
itemTitles += "<ul class='linkedimglinklist1 cf'>";

for (c = temp; c < temp + reminder; c++)
{
if (c == temp)
{
itemTitles += "<li class='first'>";
}
else
{
itemTitles += "<li>";
}

itemTitles += "<a href='" + RssNavMenu[c].Link + "'><img src='" + RssNavMenu[c].Icon + "' width='25' height='20' alt='Windows Live Messenger' /><span><strong>" + RssNavMenu[c].Title + "</strong></span></a>";

itemTitles += "</li>";
}
itemTitles += "</ul>";


//Closing list


}



return itemTitles;
}

public List<NavFeedRss> GetNavToolLinks()
{

List<NavFeedRss> rsstitles = new List<NavFeedRss>();

WebRequest myRequest = WebRequest.Create(rssUrl);
WebResponse myResponse = myRequest.GetResponse();

Stream rssStream = myResponse.GetResponseStream();
XmlDocument rssDoc = new XmlDocument();
rssDoc.Load(rssStream);



//Setting up NSManager
XmlNamespaceManager man = new XmlNamespaceManager(rssDoc.NameTable);
man.AddNamespace("msncp", "urn:schemas-microsoft-com/contentpublishing/content");


XmlNodeList rssItemsImg = rssDoc.SelectNodes("rss/channel/msncp:gtl/msncp:networklists/msncp:toollist/msncp:tool/msncp:imagelink/msncp:image", man);
XmlNodeList rssItemsLnk = rssDoc.SelectNodes("rss/channel/msncp:gtl/msncp:networklists/msncp:toollist/msncp:tool/msncp:imagelink/msncp:link", man);


string title = "";
string link = "";
string description = "";
string enclosure = "";




for (int i = 0; i < rssItemsLnk.Count; i++)
{
XmlNode rssDetail;


rssDetail = rssItemsImg.Item(i).SelectSingleNode("msncp:src", man);
if (rssDetail != null)
{
enclosure = rssDetail.InnerText;
}
else
{
enclosure = "";
}



rssDetail = rssItemsImg.Item(i).SelectSingleNode("msncp:alternatetext", man);
if (rssDetail != null)
{
description = rssDetail.InnerText;
}
else
{
description = "";
}



rssDetail = rssItemsLnk.Item(i).SelectSingleNode("msncp:text", man);
if (rssDetail != null)
{
title = rssDetail.InnerText;
}
else
{
title = "";
}


rssDetail = rssItemsLnk.Item(i).SelectSingleNode("msncp:url", man);
if (rssDetail != null)
{
link = rssDetail.InnerText;
}
else
{
link = "";
}

rsstitles.Add(new NavFeedRss(title, description, link, enclosure));

}





return rsstitles;
}


public List<NavFeedRss> GetNavLinks(int start)
{

List<NavFeedRss> rsstitles = new List<NavFeedRss>();

WebRequest myRequest = WebRequest.Create(rssUrl);
WebResponse myResponse = myRequest.GetResponse();

Stream rssStream = myResponse.GetResponseStream();
XmlDocument rssDoc = new XmlDocument();
rssDoc.Load(rssStream);

XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");

string title = "";
string link = "";
string description = "";
string enclosure = "";

for (int i = start; i < rssItems.Count; i++)
{
XmlNode rssDetail;

rssDetail = rssItems.Item(i).SelectSingleNode("title");
if (rssDetail != null)
{
title = rssDetail.InnerText;
}
else
{
title = "";
}

rssDetail = rssItems.Item(i).SelectSingleNode("enclosure");
if (rssDetail != null)
{
enclosure = rssDetail.Attributes["url"].InnerText;
}
else
{
enclosure = "";
}


rssDetail = rssItems.Item(i).SelectSingleNode("link");
if (rssDetail != null)
{
link = rssDetail.InnerText;
}
else
{
link = "";
}

rssDetail = rssItems.Item(i).SelectSingleNode("description");
if (rssDetail != null)
{
description = rssDetail.InnerText;
}
else
{
description = "";
}


rsstitles.Add(new NavFeedRss(title, description, link, enclosure));

}

return rsstitles;
}

}

19 comments:

Anonymous said...

These are truly impressive ideas in regarding blogging.
You have touched some pleasant factors here. Any way keep
up wrinting.

my web page: アグ ブーツ 通販

Anonymous said...

Very nice post. I just stumbled upon your blog and
wished to say that I have truly enjoyed surfing around your blog posts.
In any case I'll be subscribing to your rss feed and I hope you write again very soon!

Here is my webpage ... cenaless

Anonymous said...

I quite like reading through a post that can make people think.
Also, many thanks for allowing for me to comment!


My weblog: does it take to rid of cellulite on thighs

Anonymous said...

In fact, the current browser applications on the Z10 has a rating
to 480 when examined open html5test.com page.
Sometimes, after it is end to update and restarting your pc, it has
new updates.

Also visit my blog :: how to install whatsapp on pc free (http://www.futuresoft.co.kr/)

Anonymous said...

Good way of describing, and good article to take facts
concerning my presentation subject matter, which i am going
to present in academy.

Anonymous said...

A fascinating discussion is definitely worth comment.
I do believe that you need to write more about this topic,
it may not be a taboo matter but usually people do
not talk about these subjects. To the next! All the best!!



Feel free to surf to my web blog; UK online doctor
()

Anonymous said...

so this game hilariously butchers the English
language. For Beatlemaniacs, we picture this video was pretty funny.
The D-pad does use a funky design though, that might appeal
for some people, but most will most likely consider it's alien design uncomplimentary on the overall appearance and
feel from the phone.

Stop by my web site :: funny videos ()

Anonymous said...

I don't know whether it's just me or if everybody else encountering problems with your
site. It looks like some of the written text within your content are running off the screen. Can someone else
please comment and let me know if this is happening to them as
well? This may be a problem with my web browser because I've had this happen before.
Thanks

Here is my blog ... tickets

Anonymous said...

Greate article. Keep writing such kind of info
on your site. Im really impressed by your site.
Hey there, You have done a fantastic job. I'll certainly digg it and personally recommend to my friends.
I am sure they'll be benefited from this site.


Review my web site :: ingreso cybernetico products - www.youtube.com,

Anonymous said...

I have read some just right stuff here. Certainly value bookmarking for revisiting.
I surprise how much effort you set to make this sort of fantastic informative web site.



Also visit my web site june 19 hi-dive

Anonymous said...

Having read this I believed it was extremely enlightening.
I appreciate you spending some time and energy to put this informative article together.
I once again find myself personally spending a significant amount of time both reading
and leaving comments. But so what, it was still worth it!

Also visit my webpage :: tickets

Anonymous said...

Hi to every body, it's my first go to see of this webpage; this webpage contains awesome and truly excellent data in favor of readers.


My web site: tickets - ,

Anonymous said...

First off I would like to say excellent blog! I had a quick question which
I'd like to ask if you don't mind. I was interested to know how you
center yourself and clear your thoughts prior to writing.
I've had a tough time clearing my thoughts in getting my ideas out there.
I do enjoy writing but it just seems like the first 10 to 15 minutes tend to be wasted just trying to figure out how to begin. Any ideas or tips?

Thanks!

Review my web site cs1.6

Anonymous said...

ve booked your holiday, filled in your EHIC card and paid for your travel insurance.
Simply rub your finger-tips up and down about ten times and then rub
them from the corners of your eyes outwards.
From this business model Matt Law created Four Step Marketing Consultants
™.

My web page :: Chloe Sims How Do You Remove How To Remove Gel Nails

Anonymous said...

Great delivery. Solid arguments. Keep up the good spirit.


my homepage: mejor hosting 2014

Unknown said...
This comment has been removed by the author.
Anonymous said...

It's enormous that you are getting thoughts from this article as well as from
our argument made at this time.

Here is my webpage - greenville infertility augusta infertility

Anonymous said...

You just need to his normal email. Lori: How long do you foresee yourself
keeping this job. It is safe to say that most are not involved with providing any form of companionship for the patron.

My site ... Bester Escort Service Berlin ()

Anonymous said...

e cigarette forum, smokeless cigarettes, smokeless cigarettes, vapor cigarette, electronic cigarettes, electronic cigarette