Friday, February 16, 2007

Exposing Data as RSS feed

I blogged a week or so about wanting to create an RSS feed using Asp.Net from some stories we hold in a database. Well, today cos i'm off for a week and couldn't be bothered doing anything else I had a play about with it and managed to get it working.

I downloaded the example that's part of the Microsoft artical as a starting point (its here:http://msdn2.microsoft.com/en-us/library/aa478968.aspx.). My first problem was the example is written in c#, and i've never used that before. Still I rolled up my sleeves and had a go.

The first thing I had to do was alter the SQL it was running, and the database it connected to. So I altered that in the code behind file and ran the page up expecting to be presented with a nice XML file. Of course it didn't work like that!

I was presented with one of the famous .net error pages, telling me that the Codebehind property of the page directive at the top of the page was no longer supported. I had a surf around and found that it was no longer supported in .Net2.0. I had to use the CodeFile and Inherits directive. So that page directive as a whole looked like this:

<%@ Page CodeFile="rss.aspx.cs" language="c#" ContentType="text/xml" AutoEventWireup="false" Inherits="SyndicationDemo.rss"%>

Once I did that, I started getting another error. This time it was down to me not being able to connect to the database. I checked my connection string and it looked ok. I then noticed there was a red error underline in the server name. The server was an instance so it was like news-db\newsdatabase. I remembered back to my limited C++ knowledge about escape character's, and that a forward slash was used to denote one. Once I put 2 forward slashes in, it started working!

I could now browse to the site, and out came my xml. Needles to say, I was most impressed. Before trying this I'd never programmed in C#, used a data repeater control or output a page in anything but html.

I still have some work to do on the project though. Sometimes there are characters in the story filed that the XML cant handle, I need to strip these out. I need to make it do something in case the page can't connect to the database or gets an error back when retrieving data. I also want to add one of those RSS icons to the normal PA web application, like the ones that appear in the address bar of other sites. When I get these working ill post anything useful here.

Anyway, I'm of to the dog racing now, hope I done lose to much money.

Listening To: Oasis, Definitely Maybe. What a suburb album, when you don't hear it for a while you forget how good it is.

No comments: