C#
  Home arrow C# arrow Page 3 - Creating an RSS Feed with ASP.Net Written ...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Creating an RSS Feed with ASP.Net Written in C#
By: Luke Niland
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 24
    2007-07-02

    Table of Contents:
  • Creating an RSS Feed with ASP.Net Written in C#
  • Creating the Pages
  • Code Behind Page
  • The Data Repeater

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Creating an RSS Feed with ASP.Net Written in C# - Code Behind Page


    (Page 3 of 4 )

    Create a new file called rss.aspx.cs in the same place you have created the rss.aspx page.

    At the very top of the page, we will tell the pages about all the dot.net classes we will be using, as follows:

    using System;

    using System.Text;

    using System.Collections;

    using System.ComponentModel;

    using System.Data;

    using System.Data.SqlClient;

    using System.Drawing;

    using System.Xml;

    using System.Web;

    using System.Web.SessionState;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.HtmlControls;

    After that we create the namespace, and set it to be a web page

    namespace SyndicationDemo

    {

       //Class for returning RSS feed Data

       public partial class rss : System.Web.UI.Page

       {

    Now we will write the page load event. This will run every time someone requests the page. It basically queries our database for the information, and then binds this to the repeater control.

    private void Page_Load(object sender, System.EventArgs e)

    {

       // Connect to the Database

       SqlConnection myConnection = new SqlConnection("Data Source=DBServer1;Initial Catalog=rss_news;Persist Security Info=True;User ID=user1;Password=Pass;");

       // Retrieve the SQL query results and bind it to the Repeater

       const string SQL_QUERY = "SELECT top 50 id, headline, story_text, FROM news_stories";

       SqlCommand myCommand = new SqlCommand(SQL_QUERY, myConnection);

     

       myConnection.Open();

       rptRSS.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

       rptRSS.DataBind();

       myConnection.Close();

    }

    The first thing we do is create a SQL server connection to our database. In a real world production application you would want to add some exception handling to the code; it is left out here to made the code more readable.

    Once we have connected, we run a SQL query on the database to return our stories. The SQL here is using the TOP keyword so we only return a limited number of stories to show. In the real world you might limit this to stories within a certain date, of a certain status, and so on.

    The TOP keyword might not work if you are using a database that is hosted on a non-Microsoft SQL server.

    We then open the connection to the database, execute the SQL against this connection, bind the SQL queries results to the repeater and close the connections.

    The last part of the code behind page is a simple function that will be called from the main page. This function replaces any chars in your stories that are XML reserved characters

    protected string FormatForXML(object input)

    {

       string data = input.ToString(); //cast input to string

     

       // replace those characters disallowed in XML documents

       data = data.Replace("&", "&");

       data = data.Replace(""", """);

       data = data.Replace("'", "'");

       data = data.Replace("<", "&lt;");

       data = data.Replace(">", "&gt;");

     

       return data;

    }

    The function just accepts one argument, casts it to a text type and does a find and replace on the reserved characters to make them valid.

    More C# Articles
    More By Luke Niland


       · Hi, Hope you like the article and find it useful, its a fairly straight forward...
       · Great explanations. Not like most on the net. Anyway, just a small correction in...
       · Hi,Glad you liked the explanations i try to keep them as simple as possible....
       · How Aboutdata = data.Replace("\"", """);
       · However...you lost me at step 2. Not your fault, but it all looks like Greek to...
     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT