Now let’s try to find out what RSS is about. Basically the website owner should return a RSS feed - and that's simply an XML document following a certain standard, describing new or latest articles on your site. When one copy your feeds & read it by a reader like goggle reader then reader get an overview of your latest articles. If reader wants to read details lets "How to make or create RSS Feed using Asp.net" then the reader will click on your Creating RSS Feed link which will redirect the user to your site.
So i hope now you can understand why Creating RSS feed is necessary for website or blog owner. In blog we will get RSS feed by default but for website you must need to create or make RSS feed for your regular readers so that all times they won’t visit your website to read your latest articles.
In most cases developers Create RSS feed from database. That’s why I will show you the way how we can achieve it. Since you are making or creating RSS feed from database so that you can Dynamically or Runtime create RSS feed from your aspx page. To do that first create the below table:
Fig: Table Structure
Enter some data like:
Fig: Sample data
Now create a new web site. Rename the default.aspx page to CreateRSS.aspx. Now open the CreateRSS.aspx page and add the below line just after the first line of the page:
1 | <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CreateRSS.aspx.cs" Inherits="_Default" %> |
2 | <%@ OutputCache Duration="120" VaryByParam="*" %> |
Now go to code behind & write the code under page load event to dynamically create RSS feed using Asp.Net:
03 | using System.Configuration; |
04 | using System.Data.SqlClient; |
08 | public partial class _Default : System.Web.UI.Page |
10 | protected void Page_Load( object sender, EventArgs e) |
12 | string connectionString = ConfigurationManager.ConnectionStrings[ "TestConnection" ].ConnectionString; |
13 | DataTable dt = new DataTable(); |
14 | SqlConnection conn = new SqlConnection(connectionString); |
17 | SqlDataAdapter ad = new SqlDataAdapter( "SELECT * from tblRSS" , conn); |
22 | Response.ContentType = "text/xml" ; |
23 | XmlTextWriter TextWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8); |
24 | TextWriter.WriteStartDocument(); |
27 | TextWriter.WriteStartElement( "rss" ); |
28 | TextWriter.WriteAttributeString( "version" , "2.0" ); |
31 | TextWriter.WriteStartElement( "channel" ); |
32 | TextWriter.WriteElementString( "title" , ".Net Mixer Free Articles" ); |
34 | TextWriter.WriteElementString( "description" , "Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,XML,GridView Articles and code examples -- by Shawpnendu Bikash" ); |
35 | TextWriter.WriteElementString( "copyright" , "Copyright 2009 - 2010 shawpnendu.blogspot.com. All rights reserved." ); |
37 | foreach (DataRow oFeedItem in dt.Rows) |
39 | TextWriter.WriteStartElement( "item" ); |
40 | TextWriter.WriteElementString( "title" , oFeedItem[ "Title" ].ToString()); |
41 | TextWriter.WriteElementString( "description" , oFeedItem[ "Description" ].ToString()); |
42 | TextWriter.WriteElementString( "link" , oFeedItem[ "URL" ].ToString()); |
43 | TextWriter.WriteEndElement(); |
45 | TextWriter.WriteEndElement(); |
46 | TextWriter.WriteEndElement(); |
47 | TextWriter.WriteEndDocument(); |
Now build the project & run it hope you will get an output like below:
So now i hope that you can runtime create RSS feed in asp.net application without help of others. In my next article i will show you how you can Read/Consume RSS feed in your asp.net aspx page.
No comments :
Post a Comment