RSS Feed from an Access Database Scott Savage (2005) RSS stands for Really Simple Syndication and it is the latest way to keep up to date on the internet. Essentially it is an XML feed that contains data such as headlines which provides a summary of an information update. In this example I will be showing you how to use an Access Database which stores news stories. In this example I will be using Visual Web Developer 2005 Express Edition. This is a free download from Microsoft and is a very comprehensive and user friendly package for anyone looking to write.net web applications. The first step is to create a new web site in which to write our code. You may already have an existing web site, and if this is the case then just insert a new.aspx file into your current web site. We will be doing all our coding today in the source view window as shown below. As you can see I am going to put my code in the code behind file called Default.aspx.cs. I like the way Visual Studio separates your code into presentation layers into these 2 files. In our case however there will be very little going into the presentation layer (Default.aspx). This example also assumes that you have already created an Access database which is located in the App_Data directory within your website. This database is called news.mdb in my case and should have the following fields:
Field Name Field Type Field Description Newsid Number Primary key for the news articles Headline Text Stores the headline for the news article Date Date/Time Stores the time and date that the news article was published. Body Text Stores the body of the news article Notice that when you create a new aspx file the code sits in the code behind file, in this case it is called Default.aspx.cs (because I am programming in C#). This is also a good shot of the Express Edition interface. It is almost identical to the Visual Studio 2005 interface and contains all the critical features. In fact I am yet to find a single feature that I need and is not present. I strongly advise anyone looking to dip their toes into.net to download this software before Microsoft starts charging for it in a year or so. We next need to configure the database connection string. The best place to do this is in the web.config file. If you have already compiled the project for the first time a basic web.config will have already been generated for you. You can
just modify the file (which is in XML) to add in the <connectionstring> information. This is a better place to store the connection string as it can be reused for all your database connections. If you change the database or the database authentication information for example then there is a central place to modify the code. We then use this connection string to connect to the database. In this case as we are connecting to an Access database we use the OleDb connection methods. These would change based on what type of database you want to connect to (or XML source). In our SELECT query we are just selecting all the data from the news rows. In practice we would probably limit the number of results returned. The number of results to return varies, but I would suggest 10-15 is probably a good number.
//get the connection string from web.config string connectionstring = ConfigurationManager.ConnectionStrings["NewsConnectionString"].Connect ionstring; //open the database connection OleDbConnection OleDbConnection1 = new OleDbConnection(connectionString); OleDbConnection1.Open(); // Now the Select statement you want to run string select = "SELECT * FROM news ORDER BY Date"; OleDbDataAdapter oleda = new OleDbDataAdapter(select, OleDbConnection1); There are a number of headers required for setting up an RSS feed as defined in the RSS 2.0 standard. In our case we also need to setup the HTTP headers so we are responding to the HTTP request with XML. We then use the XMLTextWriter which helps us output some neat XML code for our response. // Set the content-type to XML Response.ContentType = "text/xml"; // Use an XmlTextWriter to write the XML data to a string XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8); // start the document writer.writestartdocument(); // write out <rss version="2.0"> writer.writestartelement("rss"); writer.writeattributestring("version", "2.0"); // write out <channel> writer.writestartelement("channel"); // write out <channel>-level elements writer.writeelementstring("title", "My News Site"); writer.writeelementstring("link", "http://www.mynewssite.com/"); writer.writeelementstring("description", "My News RSS Feed"); //writer.writeelementstring("ttl", "60"); writer.writeelementstring("docs","http://blogs.law.harvard.edu/tech/rs s");
Next we write out the block of XML for each news article, which is essentially the bulk of the feed. Note that the fields follow the RSS 2.0 standard set out by Harvard University. This standardisation is necessary to ensure your RSS feed is readable by 3 rd party clients. //loop through the articles and output the XML data foreach (DataTable mytable in newsdata.tables) { foreach (DataRow myrow in mytable.rows) { // write out <item> writer.writestartelement("item"); // write out <item>-level information writer.writeelementstring("title", Convert.ToString(myrow["Headline"])); // direct link to the news article writer.writeelementstring("link", "http://www.mynewssite.com/news.aspx?" + myrow["newsid"]); // the body of the news article (may need a word limit) writer.writeelementstring("description", Convert.ToString(myrow["Body"])); // the author of the news article writer.writeelementstring("author", "scott@kgbnetworks.com (Scott Savage)"); // the publish date (formatted according to RFC 822) writer.writeelementstring("pubdate", Convert.ToString(myrow["Date"])); } } // close the item tag </item> writer.writeendelement(); We have finished writing out the news articles, so it is time to clean up. We have a few elements to close off as well as closing the actual document and the HTTP response.
// clean up after we are finished with the database OleDbConnection1.Close(); // write out </channel> writer.writeendelement(); // write out </rss> writer.writeendelement(); // finish writing the document writer.writeenddocument(); // Flush and close the writer writer.flush(); writer.close(); Response.End(); This leaves us with an XML document that contains all the latest news articles for your website. An important point to note is that a URL is needed so that when a client is interested in your news article they can click a link to be taken straight there. No-one wants a link to your home page only to have to then search for the article. This will probably require you to create an additional page. One of the best things about.net is the ease with which you can modify code if a data source changes. If you would prefer to use an SQL Server or MySQL (via ODBC) database for example, the code changes are very minimal. Even using an XML data source would be a minor change. The advantage of this for a number of students, including myself, is that re-writing your front-end code in.net does not require an immediate migration of your backend (from MySQL to SQL Server for example). This kind of phased implementation is a great way to learn a lot about.net technology without throwing yourself into the deep end. With the release of IE7 and Microsoft s continued focus on web services you can be sure that your work on RSS will not go unrewarded. Content is critical to the success of any website, and a good RSS feed keeps your viewers coming back.