Click here to Skip to main content
15,893,368 members
Articles / Web Development / ASP.NET
Article

Simple RSS Builder Based on Data Table

Rate me:
Please Sign up or sign in to vote.
3.94/5 (7 votes)
14 Dec 2007CPOL2 min read 28K   368   33  
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:

EmployeeIDOrderIDAmountCostDate
Sam1251301/10/2007
Sam2512102/10/2007
Sam3512103/10/2007
Tom450104/10/2007
Tom53703/10/2007
Tom678,751202/10/2007
Sue711701/10/2007
Sue82,566,202/10/2007
Sue92,52203/10/2007
Jack1062302/10/2007
Jack1111719904/10/2007
Jack12132,601/10/2007
Jack1311,499,803/10/2007
Phill14372,102/10/2007
Phill1565,299,304/10/2007
Phill1634,12702/10/2007
Phill171795904/10/2007

The code below shows how to create the table above:

C++
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:

C++
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:

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

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

C++
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:

C++
<?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)


Written By
Software Developer I.ndigo - www.i.ndigo.com.br
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --