Now I am going to explain how to Create RSS Feed for your website
Step 1. Open/ create new Website/ Project.
Step 2. Open/ Create aspx webpage.
Step 3. add the outputcache directve in the page by pasting the following code below the page directive in the html source view
<%@ OutputCache Duration="120" VaryByParam="Group" %>
Step 4. go to code (.cs) file of the page and add the following namesspaces
//---- To establish connection with database and fetching data which is to be publish in the RSS ----
using System.Data.SqlClient;
using System.Data.SqlClient;
//--to create instance of encoding used in creating xmltextwriter
using System.Text;
using System.Text;
//-- to create xmltextwriter
using System.Xml;
using System.Xml;
Step 5. Paste the following code in the page load event.
// Clear any previous output from the buffer
Response.Clear();
Response.ContentType = "text/xml";
XmlTextWriter xtwRSSFeed = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
xtwRSSFeed.WriteStartDocument();
// The mandatory rss tag
xtwRSSFeed.WriteStartElement("rss");
xtwRSSFeed.WriteAttributeString("version", "2.0");
// The channel tag contains RSS feed details
xtwRSSFeed.WriteStartElement("channel");
xtwRSSFeed.WriteElementString("title", "Title of the RSS");
xtwRSSFeed.WriteElementString("link", "Link of the RSS");
xtwRSSFeed.WriteElementString("description", "Description of the RSS.");
xtwRSSFeed.WriteElementString("copyright", "Your Copyright Detail.");
// Objects needed for connecting to the SQL database
SqlConnection Con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ToString());
SqlCommand cmd = new SqlCommand("YourProcedureNameToGetDataFromDatabase", Con);
cmd.CommandType = CommandType.StoredProcedure;
if (Con.State == ConnectionState.Closed)
{
Con.Open();
}
SqlDataReader SqlDR = cmd.ExecuteReader();
// Loop through the content of the database and add them to the RSS feed
while (SqlDR.Read())
{
xtwRSSFeed.WriteStartElement("item");
xtwRSSFeed.WriteElementString("title", SqlDR["Title"].ToString());
xtwRSSFeed.WriteElementString("description", SqlDR["Description"].ToString());
xtwRSSFeed.WriteElementString("link", "http://www.feedpedia.com/View.aspx?View=" + SqlDR["ID"]);
xtwRSSFeed.WriteElementString("pubDate", SqlDR["Date"].ToString());
xtwRSSFeed.WriteEndElement();
}
SqlDR.Close();
Con.Close();
// Close all tags
xtwRSSFeed.WriteEndElement();
xtwRSSFeed.WriteEndElement();
xtwRSSFeed.WriteEndDocument();
xtwRSSFeed.Flush();
xtwRSSFeed.Close();
Response.End();
Response.Clear();
Response.ContentType = "text/xml";
XmlTextWriter xtwRSSFeed = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
xtwRSSFeed.WriteStartDocument();
// The mandatory rss tag
xtwRSSFeed.WriteStartElement("rss");
xtwRSSFeed.WriteAttributeString("version", "2.0");
// The channel tag contains RSS feed details
xtwRSSFeed.WriteStartElement("channel");
xtwRSSFeed.WriteElementString("title", "Title of the RSS");
xtwRSSFeed.WriteElementString("link", "Link of the RSS");
xtwRSSFeed.WriteElementString("description", "Description of the RSS.");
xtwRSSFeed.WriteElementString("copyright", "Your Copyright Detail.");
// Objects needed for connecting to the SQL database
SqlConnection Con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ToString());
SqlCommand cmd = new SqlCommand("YourProcedureNameToGetDataFromDatabase", Con);
cmd.CommandType = CommandType.StoredProcedure;
if (Con.State == ConnectionState.Closed)
{
Con.Open();
}
SqlDataReader SqlDR = cmd.ExecuteReader();
// Loop through the content of the database and add them to the RSS feed
while (SqlDR.Read())
{
xtwRSSFeed.WriteStartElement("item");
xtwRSSFeed.WriteElementString("title", SqlDR["Title"].ToString());
xtwRSSFeed.WriteElementString("description", SqlDR["Description"].ToString());
xtwRSSFeed.WriteElementString("link", "http://www.feedpedia.com/View.aspx?View=" + SqlDR["ID"]);
xtwRSSFeed.WriteElementString("pubDate", SqlDR["Date"].ToString());
xtwRSSFeed.WriteEndElement();
}
SqlDR.Close();
Con.Close();
// Close all tags
xtwRSSFeed.WriteEndElement();
xtwRSSFeed.WriteEndElement();
xtwRSSFeed.WriteEndDocument();
xtwRSSFeed.Flush();
xtwRSSFeed.Close();
Response.End();
Note. Dont forget to change the connection string in the web.config file
No comments:
Post a Comment