Sunday, August 11, 2019

How to read and display RSS FEED in asp.net?

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 

  1. <h3>Read RSS Feed from "The Times of India"</h3>
  2. <div style="max-height:350px; overflow:auto">
  3. <asp:GridView ID="gvRss" runat="server" AutoGenerateColumns="false" ShowHeader="false" Width="90%">
  4. <Columns>
  5. <asp:TemplateField>
  6. <ItemTemplate>
  7. <table width="100%" border="0" cellpadding="0" cellspacing="5">
  8. <tr>
  9. <td>
  10. <h3 style="color:#3E7CFF"><%#Eval("Title") %></h3>
  11. </td>
  12. <td width="200px">
  13. <%#Eval("PublishDate") %>
  14. </td>
  15. </tr>
  16. <tr>
  17. <td colspan="2">
  18. <hr />
  19. <%#Eval("Description") %>
  20. </td>
  21. </tr>
  22. <tr>
  23. <td>&nbsp;</td>
  24. <td align="right">
  25. <a href='<%#Eval("Link") %>' target="_blank">Read More...</a>
  26. </td>
  27. </tr>
  28. </table>
  29. </ItemTemplate>
  30. </asp:TemplateField>
  31. </Columns>
  32. </asp:GridView>
  33. </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. 

  1. private void PopulateRssFeed()
  2. {
  3. string RssFeedUrl = "http://dotnetawesome.blogspot.com/feeds/posts/default?alt=rss";
  4. List<Feeds> feeds = new List<Feeds>();
  5. try
  6. {
  7. XDocument xDoc = new XDocument();
  8. xDoc = XDocument.Load(RssFeedUrl);
  9. var items = (from x in xDoc.Descendants("item")
  10. select new
  11. {
  12. title = x.Element("title").Value,
  13. link = x.Element("link").Value,
  14. pubDate = x.Element("pubDate").Value,
  15. description = x.Element("description").Value
  16. });
  17. if (items != null)
  18. {
  19. foreach (var i in items)
  20. {
  21. Feeds f = new Feeds
  22. {
  23. Title = i.title,
  24. Link = i.link,
  25. PublishDate = i.pubDate,
  26. Description = i.description
  27. };
  28.  
  29. feeds.Add(f);
  30. }
  31. }
  32.  
  33. gvRss.DataSource = feeds;
  34. gvRss.DataBind();
  35. }
  36. catch (Exception ex)
  37. {
  38. throw;
  39. }
  40. }
Step-5: Run Application

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...