We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

gndprx on 04/17/07


Tagged


Versions (?)


ASP.NET 2.0 RSS Feed Generator


Published in: C# 


Generates an RSS XML feed from a SQL stored procedure.

Save below as feedname.aspx, modify stored procedure information to match and run. No seperate markup or cs file necessary, self contained.

This was created from a combination of articles found online.
RSS Code originally from: http://www.geekpedia.com/tutorial157_Create-an-RSS-feed-using-ASP.NET-2.0.html

C# Left code from: http://www.csharphelp.com/archives4/archive616.html

  1. <%@ Page Language="C#" %>
  2. <%@ OutputCache Duration="120" VaryByParam="Group" %>
  3.  
  4. <script runat="server">
  5. public static string Left(string param, int length)
  6. {
  7. //we start at 0 since we want to get the characters starting from the
  8. //left and with the specified lenght and assign it to a variable
  9. string result = param.Substring(0, length);
  10. //return the result of the operation
  11. return result;
  12. }
  13.  
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. // Clear any previous output from the buffer
  17. Response.Clear();
  18. Response.ContentType = "text/xml";
  19. System.Xml.XmlTextWriter xtwFeed = new System.Xml.XmlTextWriter(Response.OutputStream, Encoding.UTF8);
  20. xtwFeed.WriteStartDocument();
  21.  
  22. // The mandatory rss tag
  23. xtwFeed.WriteStartElement("rss");
  24. xtwFeed.WriteAttributeString("version", "2.0");
  25.  
  26. // The channel tag contains RSS feed details
  27. xtwFeed.WriteStartElement("channel");
  28. xtwFeed.WriteElementString("title", "The Messenger - TransamericaReinsurance.com RSS Feed");
  29. xtwFeed.WriteElementString("link", "http://www.transamericareinsurance.com");
  30. xtwFeed.WriteElementString("description", "Knowledge. Experience. Performance. The Power of Insight.");
  31. xtwFeed.WriteElementString("copyright", "Copyright 2007 Transamerica Occidental Life Insurance Company");
  32.  
  33. // Objects needed for connecting to the SQL database
  34. System.Data.SqlClient.SqlConnection SqlCon;
  35. System.Data.SqlClient.SqlCommand SqlCom;
  36. System.Data.SqlClient.SqlDataReader SqlDR;
  37.  
  38. // Edit to match your connection string
  39. SqlCon = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["TARe_DBConn"].ToString());
  40.  
  41. // Edit to match your stored procedure or SQL command
  42. SqlCom = new System.Data.SqlClient.SqlCommand("sproc_RSSFeeds", SqlCon);
  43. SqlCom.Parameters.Add(new System.Data.SqlClient.SqlParameter("@feedName", "messenger"));
  44. SqlCom.CommandType = System.Data.CommandType.StoredProcedure;
  45.  
  46. SqlCon.Open();
  47.  
  48. SqlDR = SqlCom.ExecuteReader();
  49.  
  50. // Loop through the content of the database and add them to the RSS feed
  51. while (SqlDR.Read())
  52. {
  53. xtwFeed.WriteStartElement("item");
  54. xtwFeed.WriteElementString("title", SqlDR["Title"].ToString());
  55. xtwFeed.WriteElementString("description", Left(SqlDR["Description"].ToString(), 250)+"...");
  56. xtwFeed.WriteElementString("link", "http://cnchnw9321/Media/media_associateArticle.aspx?id=" + SqlDR["ID"]);
  57. xtwFeed.WriteElementString("pubDate", SqlDR["Date"].ToString());
  58. xtwFeed.WriteEndElement();
  59. }
  60.  
  61. SqlDR.Close();
  62. SqlCon.Close();
  63.  
  64. // Close all tags
  65. xtwFeed.WriteEndElement();
  66. xtwFeed.WriteEndElement();
  67. xtwFeed.WriteEndDocument();
  68. xtwFeed.Flush();
  69. xtwFeed.Close();
  70. Response.End();
  71. }
  72. </script>

Report this snippet 

You need to login to post a comment.