asp.net (C #) RSS function to implement code _ practical skills

Source: Internet
Author: User
Tags generator httpcontext xmlns
There may be a lot of imperfect, but can be used after all, and then slowly improve!!
The following is the background code of my RSS interface, to provide my friends with the following experience:
Copy Code code as follows:

Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.Data;
Using System.Data.SqlClient;
Using System.Xml;
Using System.IO;
Using System.Web.Configuration;
public partial class Rss:System.Web.UI.Page
{
String Hosturl;
String Httphead;
protected void Page_Load (object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
Hosturl = context. Request.Url.ToString ();
Hosturl = hosturl.substring (0, Hosturl.indexof ("/", 8));
XmlTextWriter writer = new XmlTextWriter (context. Response.outputstream, System.Text.Encoding.UTF8);
Writerssprologue (writer);
Writerssheadchennel (writer);
String sql = "Select top title,id,time,content from Blog_title order by time Desc";
SqlDataReader dr = Dbconn. ExecuteReader (SQL);
while (Dr. Read ())
{
Addrssitem (writer, (DateTime) dr["Time"). ToUniversalTime ()). ToString ("R"), dr["title"]. ToString (), Hosturl, dr["content"]. ToString ());
}
Dr. Close ();
Writer. Flush ();
Writer. Close ();
Context. response.contentencoding = System.Text.Encoding.UTF8;
Context. Response.ContentType = "Text/xml";
Context. Response.Cache.SetCacheability (Httpcacheability.public);
Context. Response.End ();
}
Private XmlTextWriter Writerssprologue (XmlTextWriter writer)
{
Writer. WriteStartDocument ();
Writer. WriteStartElement ("RSS");
Writer. WriteAttributeString ("Version", "2.0");
Writer. WriteAttributeString ("Xmlns:dc", "HTTP://PURL.ORG/DC/ELEMENTS/1.1/");
Writer. WriteAttributeString ("Xmlns:trackbac", "http://madskills.com/public/xml/rss/module/trackback/");
Writer. WriteAttributeString ("Xmlns:wfw", "http://wellformedweb.org/CommentAPI/");
Writer. WriteAttributeString ("Xmlns:slash", "http://purl.org/rss/1.0/modules/slash/");
return writer;
}
Private XmlTextWriter Writerssheadchennel (XmlTextWriter writer)
{
Writer. WriteStartElement ("channel");
Writer. WriteElementString ("title", "Programming Blog" (NICKEYJ ' s blog)-the latest log ");
Writer. WriteElementString ("link", Hosturl + "/");
Writer. WriteElementString ("description", "Programming Blog" (NICKEYJ ' s blog));
Writer. WriteElementString ("Copyright", "2008 www.52bcnet.com");
Writer. WriteElementString ("Generator", "Programming blog (NICKEYJ ' s blog) RSS Generator 2.0");
return writer;
}
Private XmlTextWriter Addrssitem (XmlTextWriter writer, string pubdate, String sitemtitle, String sitemlink, String sitemd Escription)
{
Writer. WriteStartElement ("item");
Writer. WriteElementString ("title", Sitemtitle);
Writer. WriteElementString ("link", Sitemlink);
Writer. WriteElementString ("description", sitemdescription);
Writer. WriteElementString ("pubdate", pubdate);
Writer. WriteEndElement ();
return writer;
}
Private XmlTextWriter Addrssitem (XmlTextWriter writer, string Sitemtitle, String Sitemlink, String sitemdescription, BOOL Bdescascdata)
{
Writer. WriteStartElement ("item");
Writer. WriteElementString ("title", Sitemtitle);
Writer. WriteElementString ("link", Sitemlink);
if (Bdescascdata = = True)
{
Writer. WriteStartElement ("description");
Writer. Writecdata (sitemdescription);
Writer. WriteEndElement ();
}
Else
{
Writer. WriteElementString ("description", sitemdescription);
}
Writer. WriteElementString ("pubdate", DateTime.Now.ToString ("R"));
Writer. WriteEndElement ();
return writer;
}
Private XmlTextWriter writerssclosing (XmlTextWriter writer)
{
Writer. WriteEndElement ();
Writer. WriteEndElement ();
Writer. WriteEndDocument ();
return writer;
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.