|
Introduction
How to read and display RSS FEED in asp.net?Sometime we need to fetch RSS feeds from various websites and display on our web pages like news, articles etc.
In this article I am explaining how to fetch and display RSS feeds in our website using ASP.Net (C#).
Steps are followings...
Steps :
Just follow the steps and get result easily.Step - 1 : Create New Project
Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.Step-2: Add a Class.
Go to Solution Explorer > Right Click on Project under solution explorer > Add > New item > Select Class under Code > Enter Name > Add.I have added this class to store Rss Feeds into it.
Step-3: Add a page for fetch and display rss feeds data.
Html Code<h3>Read RSS Feed from "The Times of India"</h3> <div style="max-height:350px; overflow:auto"> <asp:GridView ID="gvRss" runat="server" AutoGenerateColumns="false" ShowHeader="false" Width="90%"> <Columns> <asp:TemplateField> <ItemTemplate> <table width="100%" border="0" cellpadding="0" cellspacing="5"> <tr> <td> <h3 style="color:#3E7CFF"><%#Eval("Title") %></h3> </td> <td width="200px"> <%#Eval("PublishDate") %> </td> </tr> <tr> <td colspan="2"> <hr /> <%#Eval("Description") %> </td> </tr> <tr> <td> </td> <td align="right"> <a href='<%#Eval("Link") %>' target="_blank">Read More...</a> </td> </tr> </table> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div>
Step-4: Write code for fetch rss feeds data.
Here i have get Rss form "http://timesofindia.feedsportal.com/c/33039/f/533965/index.rss", where rss structure is like this....
<rss> <channel> <item> <title></title> <link></link> <description></description> <pubDate></pubDate> </item> </channel> </rss>Write the followings code in Page_Load event for fetch and display rss feeds data.
private void PopulateRssFeed() { string RssFeedUrl = "http://dotnetawesome.blogspot.com/feeds/posts/default?alt=rss"; List<Feeds> feeds = new List<Feeds>(); try { XDocument xDoc = new XDocument(); xDoc = XDocument.Load(RssFeedUrl); var items = (from x in xDoc.Descendants("item") select new { title = x.Element("title").Value, link = x.Element("link").Value, pubDate = x.Element("pubDate").Value, description = x.Element("description").Value }); if (items != null) { foreach (var i in items) { Feeds f = new Feeds { Title = i.title, Link = i.link, PublishDate = i.pubDate, Description = i.description }; feeds.Add(f); } } gvRss.DataSource = feeds; gvRss.DataBind(); } catch (Exception ex) { throw; } }
Step-5: Run Application.
Download Live Demo