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";
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: <title>My Rss Title</title>
<link>http: </image>
<title>My Rss Title</title>
<link>http: <item>
<title>Sam - Order 1</title>
<link>http: <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: <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: <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: <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: <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: <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: <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: <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: <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: <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: <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: <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: <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: <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: <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: <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: <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
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.