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;
}
}