Introduction:
This article is about to create RSS feed in asp.net
Procedure:
To create a rss feed follow below steps here:
1.
Add below line in to rss page under the page
directive line like below :
<%@ OutputCache Duration="120" VaryByParam="*" %>
2.
Create a table with the field Id,title,description and url and
enter some dummy data like :
3. Add the below code on page load :
string connectionString = "Data
Source=name;Initial Catalog=db name;Integrated Security=True";
DataTable d_t = new DataTable();
SqlConnection conn = new SqlConnection(connectionString);
using (conn)
{
SqlDataAdapter adp = new SqlDataAdapter("SELECT * from tablename", conn);
adp.Fill(d_t);
}
Response.Clear();
Response.ContentType = "text/xml";
XmlTextWriter Text_Writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
Text_Writer.WriteStartDocument();
//Below tags are mandatory
rss
Text_Writer.WriteStartElement("rss");
Text_Writer.WriteAttributeString("version", "2.0");
// Channel tag will contain
RSS feed details
Text_Writer.WriteStartElement("channel");
Text_Writer.WriteElementString("title", " ASP.NET ");
Text_Writer.WriteElementString("link", "http://nehaprogrammer.blogspot.com");
Text_Writer.WriteElementString("description", " C#.NET,ASP.NET ");
Text_Writer.WriteElementString("copyright", "Copyright 2013 - 2014 nehaprogrammer.blogspot.com.
All rights reserved.");
foreach (DataRow oFeedItem in d_t.Rows)
{
Text_Writer.WriteStartElement("item");
Text_Writer.WriteElementString("title",
oFeedItem["Title"].ToString());
Text_Writer.WriteElementString("description",
oFeedItem["Description"].ToString());
Text_Writer.WriteElementString("link",
oFeedItem["URL"].ToString());
Text_Writer.WriteEndElement();
}
Text_Writer.WriteEndElement();
Text_Writer.WriteEndElement();
Text_Writer.WriteEndDocument();
Text_Writer.Flush();
Text_Writer.Close();
Response.End();
}
Save all and view the page in
browser. It ll work perfectly.
Thanks
No comments:
Post a Comment