Creating an RSS Feed with ASP.Net Written in C# - The Data Repeater
(Page 4 of 4 )
Now we have all the logic in place. We can finish off the rss.aspx page we started earlier. The Repeater control is already bound to our results from the query, so we use it now to output the content to the XML page. First we create the control on the page.
<asp:Repeater id="rptRSS" runat="server">
Then we use the HeaderTemplate tags to define the start of the XML file.
<HeaderTemplate>
<rss version="2.0">
<channel>
<title>NW PA Feed (TEST)</title>
<link>http://nw-editiis1/nwpafeed/</link>
<description>This is a RSS Feed</description>
</HeaderTemplate>
This is the start of the XML file, and is giving it basic information about this feed. This is very simplistic, but it is telling the RSS reader application the version of RSS to which we are writing and the title of the feed, as well as giving it a link to the feed and the feed's description.
Now we will drop the individual items into the page.
<ItemTemplate>
<item>
<title><%#FormatForXML(DataBinder.Eval(Container.DataItem,"slug"))%>
</title>
<description>
<%#FormatForXML(DataBinder.Eval(Container.DataItem, "story"))%>
</description>
<link>http://server1/story?storyid=<%#DataBinder.Eval(Container.DataItem, "id")%>
</link>
<pubdate>
<%#String.Format("{0:R}",DateTime.Now%>
</pubdate>
</item>
</ItemTemplate>
For each of the items for which we have queried the database, we call the FormatForXML function we wrote and pass it the current record and the file in which we are interested.
To get the actual data out of the repeater, we call the dataBinder.Eval method, and pass it the data item. This item can be the name of the field or its integer value from 0 depending on the order they are returned from the query.
Remember, we need to run the data through the format function to remove some of the XML reserved characters.
When writing my version of this application, I had to make a few tweaks to the format function due to my data containing some other characters that XML didn't seem to like. These included some non-Windows line endings and some extended ASCII codes. I also made sure the headlines and main story text were only up to a certain length to improve readability and encourage the user to click on the link to the main story.
We also put the current date and time in the PubDate field, but you might already have this information in your database for each story.
Now that we've gotten the main data into the repeater, we need to write the footer tags in, and close the ASP repeater control, like this:
<FooterTemplate>
</channel>
</rss>
</FooterTemplate>
</asp:Repeater>
You now should be able to test your RSS feed. Fire up a modern browser (IE7 or Firefox; IE 6 will just display the XML) and navigate to the web URL on your web server where you dropped the code. You should be able to subscribe to the page and see all the news items.
If you click on one of your items it will take you off to the URL you specified in the link section of your XML repeater code.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |