asp.net(c#) RSS功能实现代码
程序员文章站
2024-01-28 14:18:40
可能还有很多未完善,但终归可以使用了,以后再慢慢改进!! 以下是我rss界面的后台代码,给需要的朋友提供下我的经验: 复制代码 代码如下:using system;...
可能还有很多未完善,但终归可以使用了,以后再慢慢改进!!
以下是我rss界面的后台代码,给需要的朋友提供下我的经验:
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", "编程博客(nickeyj's blog) - 最新日志");
writer.writeelementstring("link", hosturl + "/ ");
writer.writeelementstring("description", "编程博客(nickeyj's blog)");
writer.writeelementstring("copyright", "2008 www.52bcnet.com");
writer.writeelementstring("generator", "编程博客(nickeyj's blog) rss 生成器 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;
}
}
以下是我rss界面的后台代码,给需要的朋友提供下我的经验:
复制代码 代码如下:
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", "编程博客(nickeyj's blog) - 最新日志");
writer.writeelementstring("link", hosturl + "/ ");
writer.writeelementstring("description", "编程博客(nickeyj's blog)");
writer.writeelementstring("copyright", "2008 www.52bcnet.com");
writer.writeelementstring("generator", "编程博客(nickeyj's blog) rss 生成器 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;
}
}
上一篇: Photoshop制作创意的球形视觉海报
下一篇: 如何将MSN最小化到右下角的图文步骤