Search content within the blog

Friday, December 26, 2008

Displaying RSS Feeds in Asp.Net

private void SetNews()
{
string[] strFeeds = ProcessRSSItem("http://www.loobalee.com/blog/feed/rss/", 1);
if(strFeeds.Length>1)
{
lblMessageXML.Text = "<b>" + strFeeds[0] + "</b><br/>" + strFeeds[1];
}
}

public static string[] ProcessRSSItem(string rssURL, int requiredItems)
{
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[] toreturn;
if (requiredItems <= 0 || requiredItems > rssItems.Count)
{
requiredItems = rssItems.Count;
}

for (int i = 0; i < requiredItems; i++)
{
XmlNode rssDetail;

rssDetail = rssItems.Item(i).SelectSingleNode("title");
if (rssDetail != null)
{
title = rssDetail.InnerText;
}
else
{
title = "";
}
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 = "";
}

//Response.Write("<p><b><a href='" + link + "' target='new'>" + title + "</a></b><br/>");
//Response.Write(description + "</p>");
}
return new string[] { title, description };
}

No comments:

Post a Comment