|
Total Views :
55330 |
|
Adding to your Favorites....
|
|
|
 |
|
|
|
|
|
Introduction |
|
This article explains the concept of creating RSS feeds to the dynamically updating
and releasing contents in any Asp.Net website and display the RSS feeds in a browser
friendly design with the help of XSLT. |
|
|
Many of the web application now days update their website contents frequently to
attract the users at the maximum extent. Even web publishers needs to share their
content without re-creating it on any form. But there is no way to inform the users
about the new contents in their website. Either they have send newsletters to the
users or the users have to bookmark the website URL in their browser’s favorites,
which had its own drawbacks. To overcome this
difficulty, Netscape introduced the
concept of RSS in late 90s. It serves the information consumers and publishers to
stay up to date with their desired website's content.
|
|
|
What is RSS?
RSS, can be expanded as Rich Site Summary or Resource Description Framework Site
Summary or Really Simple Syndication. RSS is a lightweight XML format for distributing
regularly changed web contents among different web sites and users. A Web site can
allow the users or other sites to publish some of its content by creating an RSS
feeds.
Benefits of RSS Feeds
RSS allows the users to easily stay informed by retrieving the latest content from
the sites they are interested in. It saves time by not needing to visit each site
individually. It also ensures the privacy of the users by not needing to join the
site's email newsletter. The number of sites offering RSS feeds is growing rapidly
and includes big names like Microsoft and Asp.Net websites.
|
|
|
Basic Structure of RSS Feeds
As we explained earlier, RSS is nothing but creation of a well-formed XML file in
a pre-defined format. The main tags are shown below without which you cannot say
it as RSS.
|
<rss version="2.0">
<channel>
......
</channel>
</rss>
|
|
<rss> tag is the global container of the xml element, openly said it is the
root element. <channel> tag
contains one or many description tags such as
<title>, <description>, <link> and mainly <item> tag which
is very important. With all these tags the RSS will look like
|
<rss version="2.0">
<channel>
<title>AspdotnetCodes</title>
<link>http://www.aspdotnetcodes.com/</link>
<description>Latest Articles List in Asp.Net</description>
<item>
........
</item>
</channel>
</rss> |
|
The above tag details are
<title> provides the title of the channel or the website.
<link> provides the url of the website that provided the RSS.
<description> provides some content from the RSS provider.
<item> very important tag to keep in mind, every RSS must
contain atleast one <item> tag to provide content. This <item> tag must
contain some child tags such as <title>, <description>, <link>,
and <pubDate>, etc. With these tags a full RSS format will look like |
|
|
<rss version="2.0">
<channel>
<title>AspdotnetCodes</title>
<link>http://www.aspdotnetcodes.com/</link>
<description>Latest Articles List in Asp.Net</description>
<item>
<title>Create RSS to your website</title>
<link>http://www.yourwebsite.com/createrss.html</link>
<description>... some text... </description>
</item>
</channel>
</rss> |
|
The <item> tag's child nodes are
<title> provides the Title of the article.
<link> provides the URL of the page.
<description> provides the summary of the article.
There are some optional tags, which we cannot explain here since it is out of scope
of the article.
This article helps you to create your own RSS feeds to your website for your desired
content in Asp.Net 2.0 with the help of XMLTextWriter class and to embed the XML
with XSLT to display your feed in a browser friendly design and layouts. We have
explained it as simple as possible, just follow the steps below
|
Step 1: Create a new XSL file in you application and name it as
rss.xsl, design it on your own way if you are familiar with XSL or you can use our sample rss.xsl, we created for you.
Step 2: Create an Aspx Page with separate code-behind file and
name it as RSS.aspx.
Step 3: Open the RSS.aspx, remove all the
html and asp tags you
found, except the page directive. So your aspx page will look like
|
|
|
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Rss.aspx.cs" Inherits="Rss"
Title="My First RSS Feeds" %> |
Step 4: Open your code-behind file RSS.aspx.cs, add the following namespaces
|
using System.Data.SqlClient;
using System.Xml;
using System.Text; |
|
Step 5: In the Page_Load event, start to write the code by clearing
the server response and specify the response content type should be in XML format
as follows
Response.Clear();
Response.ContentType = "text/xml"; |
Step 6: Next we are going to create XMLTextWriter object to create
the XML document to output the XML as an OutputStream with encoding utf-8.
|
XmlTextWriter xtw = new XmlTextWriter(Response.OutputStream, Encoding.UTF8); |
Step 7: Now we can start to build the XML document with the following
line of code
|
xtw.WriteStartDocument(); |
Step 8: Next we have to create the XML processing instructions
tag. This step helps you to bind your XML with the XSLT file you created in Step
1.
string processtext = "type=\"text/xsl\" href=\"rss.xsl\"";
xtw.WriteProcessingInstruction("xml-stylesheet", processtext); |
Step 9: Now you can create the neccessary tags of the RSS as follows
xtw.WriteStartElement("rss");
xtw.WriteAttributeString("version", "2.0");
xtw.WriteStartElement("channel");
xtw.WriteElementString("title","Latest Articles List");
xtw.WriteElementString("link", "http://www.yourwebsite.com/");
xtw.WriteElementString("description", "some description here"); |
Step 10: In this step you are going to create the main
-
tag and its elements. As explained earlier, these
-
tags, are responsible for delivering the content informtion to the users. Here you have to fetch all the records from the database that you want to include in your
feed and loop through them to create the
-
elements. Look at the code below
string sql = "Select * from Articles Order by Created_Date Desc";
SqlDataAdapter da = new SqlDataAdapter(sql,”Your Connection String”);
DataTable dt = new DataTable();
da.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
xtw.WriteStartElement("item");
xtw.WriteElementString("title", dt.Rows[i]["Title"].ToString());
xtw.WriteElementString("description",dt.Rows[i]["summary"].ToString());
xtw.WriteElementString("link",dt.Rows[i]["Url"].ToString());
xtw.WriteElementString("pubDate",
XmlConvert.ToString(Convert.ToDateTime(dt.Rows[i]["Created_Date "].ToString())));
xtw.WriteEndElement();
}
|
Step 11: Close all the opened elements in the above steps and close
XMLTextWriter class as follows
xtw.WriteEndElement();
xtw.WriteEndElement();
xtw.WriteEndDocument();
xtw.Flush();
xtw.Close();
|
Done. Save your code and view this page in your browser. You must say “Yes, I’ve
done it.”. For explanation purpose, we have to separate the source code section
by section, you can view the full source code without break in the next page for
free.
Click here to view Sample Source Code
Click here to view or add our articles RSS Feed |
|
|
|
|
|
|
|
|