In this article, I will show how to create RSS feed Reader from Linq to XML in asp.net. You can fetch the feed link using the following News blog feed Link.
RSS feed link:
http://www.aljazeera.com/xml/rss/all.xml
Step 1:
Create an asp.net project and create a webform and name it as RssReader.aspx. Copy and paste the following code in desing page.
<form id="form1" runat="server">
<div>
<strong>RSS Feed:</strong><br />
<asp:TextBox ID="txtrss" runat="server" TextMode="Url" Width="500px" placeholder="Enter RSS URL"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnRSSFeed" runat="server" Text="Get RSS Feed" OnClick="btnRSSFeed_Click" Style="height: 26px" />
</div>
</form>
Step 2:
Press F7, in the code behind page RssReader.aspx.cs. Copy and paste the following code.
protected void btnRSSFeed_Click(object sender, EventArgs e)
{
var posts = GetFeeds(txtrss.Text);
StringBuilder sb = new StringBuilder();
sb.Append("<p style='font-weight:larger'><b>Latest News Content</b></p>");
foreach (var item in posts)
{
sb.Append("<b>Title: </b>" + item.title);
sb.Append("<br />");
sb.Append("<b>Description: </b>" + item.desc);
sb.Append("<br />");
sb.Append("<b>Article Link: </b><a target='_blank' href='" + item.contentlink + "'>" + item.contentlink + "</a>");
sb.Append("<br />");
sb.Append("<b>Published Date: </b>" + item.pubDate);
sb.Append("<br />");
sb.Append("------------------------------------------------------------------------------------------------------------");
sb.Append("<br />");
}
HttpContext.Current.Response.Write(sb);
}
public static IEnumerable<NewsRead> GetFeeds(string url)
{
XDocument rssfeedxml;
XNamespace namespaceName = "http://www.w3.org/2005/Atom";
rssfeedxml = XDocument.Load(url);
StringBuilder rssContent = new StringBuilder();
var list = (from descendant in rssfeedxml.Descendants("item")
select new NewsRead
{
title = descendant.Element("title").Value,
desc = descendant.Element("description").Value,
contentlink = descendant.Element("link").Value,
pubDate= descendant.Element("pubDate").Value,
});
return list.ToList();
}
Run the application and paste the RSS feed Link URL in the TextBox and Click the Get RSS Feed Button.
After the button click, rss feed links are loaded on the page.