Code for implementing the Asp.net (C #) RSS function
Last Update:2018-12-08
Source: Internet
Author: User
There may still be many incomplete ones, but they can be used all the time and will be improved later !!
Below is the background of my RSS Interface Code To provide my experience to friends in need:
Copy code The Code is 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 10 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)-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 sitemdescription)
{
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;
}
}