Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

ASP.NET: Create an RSS Feed

From Wiki

Jump to: navigation, search

Summary: Add an RSS Feed to your site

RSS feeds can be a useful way of letting your visitors see new content from your site. For example, I've added a couple on this site, one of which shows you a list of articles.

To create this on your site, add a new page entitled "rssfeed.aspx" and add this to the Page Load event:

  1. ' Declarations    
  2. Dim dtList As New DataTable
  3. Dim intCount As Integer = 10 ' Number of records to show  
  4.  
  5. ' Set the content attributes  
  6. Response.ContentType = "text/xml"  
  7. Response.ContentEncoding = Encoding.UTF8  
  8.  
  9. ' Use an XmlTextWriter to write the XML data to a string...  
  10. Dim sw As New StringWriter
  11. Dim writer As New XmlTextWriter(sw)
  12.  
  13. ' write out the rss and version  
  14. writer.WriteStartElement("rss")  
  15. writer.WriteAttributeString("version", "2.0")  
  16.  
  17. ' write out <channel>  
  18. writer.WriteStartElement("channel")  
  19.  
  20. ' write out <channel>-level elements  
  21. writer.WriteElementString("title", "ASP.NET Library - Articles")  
  22. writer.WriteElementString("link", "http://aspnetlibrary.com")  
  23. writer.WriteElementString("description", "A list of articles from the ASP.NET Library")  
  24. writer.WriteElementString("ttl", "60")  
  25.  
  26. ' Insert your code here to get the data  
  27. dtList = ...  
  28.  
  29. ' write out an <item> element for each result  
  30. For i As Integer = 0 To (intCount - 1)  
  31.  
  32. ' write out the start <item> element  
  33.     writer.WriteStartElement("item")  
  34.  
  35.     writer.WriteElementString("title", dtList.Rows(i).Item("MyTitleField").ToString)  
  36.     writer.WriteElementString("link", "http://aspnetlibrary.com/articledetails.aspx?articleid=" & dtList.Rows(i).Item("MyIDField").ToString)  
  37.     writer.WriteElementString("description", dtList.Rows(i).Item("MyDescriptionField").ToString)  
  38.     writer.WriteElementString("author", "Mark Smith")  
  39.     writer.WriteElementString("posted", dtList.Rows(i).Item("MyCreatedOnField").ToString)  
  40.  
  41. ' write out closing </item> element  
  42.     writer.WriteEndElement()  
  43.  
  44. Next  
  45.  
  46. ' write out closing </channel> element  
  47. writer.WriteEndElement()  
  48.  
  49. ' write out closing </rss> element  
  50. writer.WriteEndElement()  
  51.  
  52. ' close the writer  
  53. writer.Close()  
  54.  
  55. ' write out the xml directives and the rss feed string  
  56. Response.Write("<?xml version=""1.0"" encoding=""ISO-8859-1""?>")  
  57. ' You can also specify an xsl sheet if needed  
  58. ' Response.Write("<?xml-stylesheet type=""text/xsl"" href=""~/stylesheets/rssTemplate.xsl""?>")  
  59. Response.Write(sw.ToString())  
  60.  
  61. ' Clean Up  
  62. writer = Nothing  
  63. sw = Nothing

As I mentioned above, you can see this in action here so to get a working example on your site, you'll need to:

1. Add the code that you use to connect to your database (in the section with the comment "Insert your code here to get the data").

2. Customise the sections where I have referenced my site and set the database fields in the "item element" loop.

3. Make sure you import the System.IO, System.Xml and System.Data classes.


This Hack is part of the ASP.NET Hacks collection

471 Rating: 3.5/5 (6 votes cast)