Posted By

krisdb on 05/28/09


Tagged

c-sharp


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

poldaker
umang_nine
techdetours
franksmile0310
jalalx


Parse RSS generic lists


 / Published in: C#
 

  1. <asp:Repeater ID="rptRSS" runat="server">
  2. <ItemTemplate>
  3. <div><a href='<%# Eval("Link") %>'><%# Eval("Title") %></a></div>
  4. </ItemTemplate>
  5. </asp:Repeater>
  6.  
  7. rptRSS.DataSouce = ParseRSS("http://www.url.com/feed.rss", 3);
  8. rptRSS.DataBind();
  9.  
  10. private List<RSS> ParseRSS(string feedURL, int MaxItems)
  11. {
  12. XmlDocument xmlDoc = new XmlDocument();
  13. xmlDoc.Load(feedURL);
  14.  
  15. XmlNodeList nodeList = xmlDoc.SelectNodes("/rss/channel/item");
  16.  
  17. List<RSS> rss = new List<RSS>();
  18.  
  19. int count = 0;
  20. foreach (XmlNode node in nodeList)
  21. {
  22. if (count++ == MaxItems)
  23. break;
  24.  
  25. rss.Add(AddRSSInfo(node));
  26. }
  27.  
  28. return rss;
  29. }
  30.  
  31. public RSS AddRSSInfo(XmlNode node)
  32. {
  33. RSS rss = new RSS();
  34.  
  35. if (node.SelectSingleNode("title") != null)
  36. rss.Title = node.SelectSingleNode("title").InnerText;
  37.  
  38. if (node.SelectSingleNode("description") != null)
  39. rss.Description = node.SelectSingleNode("description").InnerText;
  40.  
  41. if (node.SelectSingleNode("link") != null)
  42. rss.Link = node.SelectSingleNode("link").InnerText;
  43.  
  44. if (node.SelectSingleNode("pubDate") != null)
  45. rss.PubDate = DateTime.Parse(node.SelectSingleNode("pubDate").InnerText).ToString("MMMM dd. hh:ss");
  46.  
  47.  
  48. return rss;
  49.  
  50. }
  51.  
  52.  
  53. public class RSS
  54. {
  55. private string _Title = String.Empty;
  56. public string Title
  57. {
  58. get { return _Title; }
  59. set { _Title = value; }
  60. }
  61.  
  62. private string _Description = String.Empty;
  63. public string Description
  64. {
  65. get { return _Description; }
  66. set { _Description = value; }
  67. }
  68.  
  69. private string _Link = String.Empty;
  70. public string Link
  71. {
  72. get { return _Link; }
  73. set { _Link = value; }
  74. }
  75.  
  76. private string _PubDate = String.Empty;
  77. public string PubDate
  78. {
  79. get { return _PubDate; }
  80. set { _PubDate = value; }
  81. }
  82.  
  83. public RSS()
  84. {
  85. this.Title = String.Empty;
  86. this.Description = String.Empty;
  87. this.Link = String.Empty;
  88. this.PubDate = String.Empty;
  89. }
  90.  
  91. }

Report this snippet  

You need to login to post a comment.