Save below as feedname.aspx, modify stored procedure information to match and run. No seperate markup or cs file necessary, self contained.
This was created from a combination of articles found online. RSS Code originally from: http://www.geekpedia.com/tutorial157_Create-an-RSS-feed-using-ASP.NET-2.0.html
C# Left code from: http://www.csharphelp.com/archives4/archive616.html
<%@ Page Language="C#" %>
<%@ OutputCache Duration="120" VaryByParam="Group" %>
public static string Left(string param, int length)
{
//we start at 0 since we want to get the characters starting from the
//left and with the specified lenght and assign it to a variable
string result = param.Substring(0, length);
//return the result of the operation
return result;
}
protected void Page_Load(object sender, EventArgs e)
{
// Clear any previous output from the buffer
Response.Clear();
Response.ContentType = "text/xml";
System.Xml.XmlTextWriter xtwFeed = new System.Xml.XmlTextWriter(Response.OutputStream, Encoding.UTF8);
xtwFeed.WriteStartDocument();
// The mandatory rss tag
xtwFeed.WriteStartElement("rss");
xtwFeed.WriteAttributeString("version", "2.0");
// The channel tag contains RSS feed details
xtwFeed.WriteStartElement("channel");
xtwFeed.WriteElementString("title", "The Messenger - TransamericaReinsurance.com RSS Feed");
xtwFeed.WriteElementString("link", "http://www.transamericareinsurance.com");
xtwFeed.WriteElementString("description", "Knowledge. Experience. Performance. The Power of Insight.");
xtwFeed.WriteElementString("copyright", "Copyright 2007 Transamerica Occidental Life Insurance Company");
// Objects needed for connecting to the SQL database
System.Data.SqlClient.SqlConnection SqlCon;
System.Data.SqlClient.SqlCommand SqlCom;
System.Data.SqlClient.SqlDataReader SqlDR;
// Edit to match your connection string
SqlCon = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["TARe_DBConn"].ToString());
// Edit to match your stored procedure or SQL command
SqlCom = new System.Data.SqlClient.SqlCommand("sproc_RSSFeeds", SqlCon);
SqlCom.Parameters.Add(new System.Data.SqlClient.SqlParameter("@feedName", "messenger"));
SqlCom.CommandType = System.Data.CommandType.StoredProcedure;
SqlCon.Open();
SqlDR = SqlCom.ExecuteReader();
// Loop through the content of the database and add them to the RSS feed
while (SqlDR.Read())
{
xtwFeed.WriteStartElement("item");
xtwFeed.WriteElementString("title", SqlDR["Title"].ToString());
xtwFeed.WriteElementString("description", Left(SqlDR["Description"].ToString(), 250)+"...");
xtwFeed.WriteElementString("link", "http://cnchnw9321/Media/media_associateArticle.aspx?id=" + SqlDR["ID"]);
xtwFeed.WriteElementString("pubDate", SqlDR["Date"].ToString());
xtwFeed.WriteEndElement();
}
SqlDR.Close();
SqlCon.Close();
// Close all tags
xtwFeed.WriteEndElement();
xtwFeed.WriteEndElement();
xtwFeed.WriteEndDocument();
xtwFeed.Flush();
xtwFeed.Close();
Response.End();
}
No comments:
Post a Comment