Click here to Skip to main content
Click here to Skip to main content

Simple RSS Builder Based on Data Table

, 14 Dec 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
This article explains how to read a Datatable using an RSS Configurator Object to return a valid XmlDocument
Screenshot -

Introduction

Displaying your data in an RSS format might be a lot of work if you have lots of different formats and kinds of data to display, for example a portal or a huge Web site, and you have to configurate and program all RSS for each section.

This article gives the idea of a generic RSS control to optimize this work and reduces the need to care about the XML tags every time you want to build an RSS feed.

Background

The main idea is to have the BuidXML method based on a DataTable and a RssConfigurator to create the RSS XML correctly.

Based on this idea, you don't have to care about the RSS and XML tags, you simply use the RSS configurator to express how you want your RSS feed to be created.

Using the Code

The RSS Configurator has some Expression Properties such as ExpressionDate, ExpressionTitle, ExpressionDescription and ExpressionTitle to configure the properties of each item of the RSS and the Properties Title, ImageUrl and Link are used to configure the link RSS itself.

The DataTable below is used as an example to create an RSS feed:

EmployeeID OrderID Amount Cost Date
Sam 1 25 13 01/10/2007
Sam 2 512 1 02/10/2007
Sam 3 512 1 03/10/2007
Tom 4 50 1 04/10/2007
Tom 5 3 7 03/10/2007
Tom 6 78,75 12 02/10/2007
Sue 7 11 7 01/10/2007
Sue 8 2,5 66,2 02/10/2007
Sue 9 2,5 22 03/10/2007
Jack 10 6 23 02/10/2007
Jack 11 117 199 04/10/2007
Jack 12 13 2,6 01/10/2007
Jack 13 11,4 99,8 03/10/2007
Phill 14 37 2,1 02/10/2007
Phill 15 65,2 99,3 04/10/2007
Phill 16 34,1 27 02/10/2007
Phill 17 17 959 04/10/2007

The code below shows how to create the table above:

DataTable dt = new DataTable();

dt.Columns.Add("EmployeeID", Type.GetType("System.String"));
dt.Columns.Add("OrderID", Type.GetType("System.Int32"));
dt.Columns.Add("Amount", Type.GetType("System.Decimal"));
dt.Columns.Add("Cost", Type.GetType("System.Decimal"));
dt.Columns.Add("Date", Type.GetType("System.String"));
dt.Rows.Add(new object[] { "Sam", 1, 25.00, 13.00, "01/10/2007" });
dt.Rows.Add(new object[] { "Sam", 2, 512.00, 1.00, "02/10/2007" });
dt.Rows.Add(new object[] { "Sam", 3, 512.00, 1.00, "03/10/2007" });
dt.Rows.Add(new object[] { "Tom", 4, 50.00, 1.00, "04/10/2007" });
dt.Rows.Add(new object[] { "Tom", 5, 3.00, 7.00, "03/10/2007" });
dt.Rows.Add(new object[] { "Tom", 6, 78.75, 12.00, "02/10/2007" });
dt.Rows.Add(new object[] { "Sue", 7, 11.00, 7.00, "01/10/2007" });
dt.Rows.Add(new object[] { "Sue", 8, 2.50, 66.20, "02/10/2007" });
dt.Rows.Add(new object[] { "Sue", 9, 2.50, 22.00, "03/10/2007" });
dt.Rows.Add(new object[] { "Jack", 10, 6.00, 23.00, "02/10/2007" });
dt.Rows.Add(new object[] { "Jack", 11, 117.00, 199.00, "04/10/2007" });
dt.Rows.Add(new object[] { "Jack", 12, 13.00, 2.60, "01/10/2007" });
dt.Rows.Add(new object[] { "Jack", 13, 11.40, 99.80, "03/10/2007" });
dt.Rows.Add(new object[] { "Phill", 14, 37.00, 2.10, "02/10/2007" });
dt.Rows.Add(new object[] { "Phill", 15, 65.20, 99.30, "04/10/2007" });
dt.Rows.Add(new object[] { "Phill", 16, 34.10, 27.00, "02/10/2007" });
dt.Rows.Add(new object[] { "Phill", 17, 17.00, 959.00, "04/10/2007" });

So, to build the RSS, you should only configurate the RSSConfigurator in the way you want.

Here is an example of an RSS feed configurated:

RssBuilder.RssConfigurator configurator = new RssBuilder.RssConfigurator();
configurator.Title = "My Rss Title";
configurator.Link = "http://myrss.com";
configurator.ImageUrl = "http://myrss.com/image.gif";
configurator.ExpressionDate = "[Date]";
configurator.ExpressionDescription = "Cost: <b>[Cost]</b><br> Amount: <b>[Amount]</b>";
configurator.ExpressionLink = "http://myrss.com/?orderId=[OrderID]";
configurator.ExpressionTitle = "[EmployeeID] - Order [OrderID]";

The return of the BuildXML method is an XmlDocument object:

System.Xml.XmlDocument xmlDoc = RssBuilder.RssBuilder.BuildXML(table, configurator);

Write the XML on your Web page and make the content XML readable:

Response.ClearContent();
Response.ContentType = "text/xml";
//Gets the XML String from XmlDocument
Response.Write(xmlDoc.OuterXml);

In this way, the RSS returned looks like this:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <image>
      <url>http://myrss.com/image.gif</url>
      <title>My Rss Title</title>
      <link>http://myrss.com</link>
    </image>
    <title>My Rss Title</title>
    <link>http://myrss.com</link>
    <item>
      <title>Sam - Order 1</title>
      <link>http://myrss.com/?orderId=1</link>
      <pubDate>01/10/2007</pubDate>
      <description><![CDATA[Cost: <b>13</b><br> Amount: <b>25</b>]]></description>
    </item>
    <item>
      <title>Sam - Order 2</title>
      <link>http://myrss.com/?orderId=2</link>
      <pubDate>02/10/2007</pubDate>
      <description><![CDATA[Cost: <b>1</b><br> Amount: <b>512</b>]]></description>
    </item>
    <item>
      <title>Sam - Order 3</title>
      <link>http://myrss.com/?orderId=3</link>
      <pubDate>03/10/2007</pubDate>
      <description><![CDATA[Cost: <b>1</b><br> Amount: <b>512</b>]]></description>
    </item>
    <item>
      <title>Tom - Order 4</title>
      <link>http://myrss.com/?orderId=4</link>
      <pubDate>04/10/2007</pubDate>
      <description><![CDATA[Cost: <b>1</b><br> Amount: <b>50</b>]]></description>
    </item>
    <item>
      <title>Tom - Order 5</title>
      <link>http://myrss.com/?orderId=5</link>
      <pubDate>03/10/2007</pubDate>
      <description><![CDATA[Cost: <b>7</b><br> Amount: <b>3</b>]]></description>
    </item>
    <item>
      <title>Tom - Order 6</title>
      <link>http://myrss.com/?orderId=6</link>
      <pubDate>02/10/2007</pubDate>
      <description><![CDATA[Cost: <b>12</b><br> Amount: <b>78,75</b>]]></description>
    </item>
    <item>
      <title>Sue - Order 7</title>
      <link>http://myrss.com/?orderId=7</link>
      <pubDate>01/10/2007</pubDate>
      <description><![CDATA[Cost: <b>7</b><br> Amount: <b>11</b>]]></description>
    </item>
    <item>
      <title>Sue - Order 8</title>
      <link>http://myrss.com/?orderId=8</link>
      <pubDate>02/10/2007</pubDate>
      <description><![CDATA[Cost: <b>66,2</b><br> Amount: <b>2,5</b>]]></description>
    </item>
    <item>
      <title>Sue - Order 9</title>
      <link>http://myrss.com/?orderId=9</link>
      <pubDate>03/10/2007</pubDate>
      <description><![CDATA[Cost: <b>22</b><br> Amount: <b>2,5</b>]]></description>
    </item>
    <item>
      <title>Jack - Order 10</title>
      <link>http://myrss.com/?orderId=10</link>
      <pubDate>02/10/2007</pubDate>
      <description><![CDATA[Cost: <b>23</b><br> Amount: <b>6</b>]]></description>
    </item>
    <item>
      <title>Jack - Order 11</title>
      <link>http://myrss.com/?orderId=11</link>
      <pubDate>04/10/2007</pubDate>
      <description><![CDATA[Cost: <b>199</b><br> Amount: <b>117</b>]]></description>
    </item>
    <item>
      <title>Jack - Order 12</title>
      <link>http://myrss.com/?orderId=12</link>
      <pubDate>01/10/2007</pubDate>
      <description><![CDATA[Cost: <b>2,6</b><br> Amount: <b>13</b>]]></description>
    </item>
    <item>
      <title>Jack - Order 13</title>
      <link>http://myrss.com/?orderId=13</link>
      <pubDate>03/10/2007</pubDate>
      <description><![CDATA[Cost: <b>99,8</b><br> Amount: <b>11,4</b>]]></description>
    </item>
    <item>
      <title>Phill - Order 14</title>
      <link>http://myrss.com/?orderId=14</link>
      <pubDate>02/10/2007</pubDate>
      <description><![CDATA[Cost: <b>2,1</b><br> Amount: <b>37</b>]]></description>
    </item>
    <item>
      <title>Phill - Order 15</title>
      <link>http://myrss.com/?orderId=15</link>
      <pubDate>04/10/2007</pubDate>
      <description><![CDATA[Cost: <b>99,3</b><br> Amount: <b>65,2</b>]]></description>
    </item>
    <item>
      <title>Phill - Order 16</title>
      <link>http://myrss.com/?orderId=16</link>
      <pubDate>02/10/2007</pubDate>
      <description><![CDATA[Cost: <b>27</b><br> Amount: <b>34,1</b>]]></description>
    </item>
    <item>
      <title>Phill - Order 17</title>
      <link>http://myrss.com/?orderId=17</link>
      <pubDate>04/10/2007</pubDate>
      <description><![CDATA[Cost: <b>959</b><br> Amount: <b>17</b>]]></description>
    </item>
  </channel>
</rss>

Points of Interest

RSS specification:

RSS validator:

If you would like to know more about RSS, follow the links below:

History

  • 13/12/2007 - First release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

felipesabino
Software Developer I.ndigo - www.i.ndigo.com.br
Brazil Brazil
No Biography provided
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 14 Dec 2007
Article Copyright 2007 by felipesabino
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid