asp.net(c#) RSS功能实现代码_asp.net教程-查字典教程网
asp.net(c#) RSS功能实现代码
asp.net(c#) RSS功能实现代码
发布时间:2016-12-29 来源:查字典编辑
摘要:可能还有很多未完善,但终归可以使用了,以后再慢慢改进!!以下是我RSS界面的后台代码,给需要的朋友提供下我的经验:复制代码代码如下:usin...

可能还有很多未完善,但终归可以使用了,以后再慢慢改进!!

以下是我RSS界面的后台代码,给需要的朋友提供下我的经验:

复制代码 代码如下:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Collections;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

usingSystem.Data;

usingSystem.Data.SqlClient;

usingSystem.Xml;

usingSystem.IO;

usingSystem.Web.Configuration;

publicpartialclassrss:System.Web.UI.Page

{

stringHostUrl;

stringHttpHead;

protectedvoidPage_Load(objectsender,EventArgse)

{

HttpContextcontext=HttpContext.Current;

HostUrl=context.Request.Url.ToString();

HostUrl=HostUrl.Substring(0,HostUrl.IndexOf("/",8));

XmlTextWriterwriter=newXmlTextWriter(context.Response.OutputStream,System.Text.Encoding.UTF8);

WriteRSSPrologue(writer);

WriteRSSHeadChennel(writer);

stringsql="selecttop10title,id,time,contentfromblog_titleorderbytimedesc";

SqlDataReaderdr=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();

}

privateXmlTextWriterWriteRSSPrologue(XmlTextWriterwriter)

{

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/");

returnwriter;

}

privateXmlTextWriterWriteRSSHeadChennel(XmlTextWriterwriter)

{

writer.WriteStartElement("channel");

writer.WriteElementString("title","编程博客(Nickeyj'sBlog)-最新日志");

writer.WriteElementString("link",HostUrl+"/");

writer.WriteElementString("description","编程博客(Nickeyj'sBlog)");

writer.WriteElementString("copyright","2008www.52bcnet.com");

writer.WriteElementString("generator","编程博客(Nickeyj'sBlog)RSS生成器2.0");

returnwriter;

}

privateXmlTextWriterAddRSSItem(XmlTextWriterwriter,stringpubDate,stringsItemTitle,stringsItemLink,stringsItemDescription)

{

writer.WriteStartElement("item");

writer.WriteElementString("title",sItemTitle);

writer.WriteElementString("link",sItemLink);

writer.WriteElementString("description",sItemDescription);

writer.WriteElementString("pubDate",pubDate);

writer.WriteEndElement();

returnwriter;

}

privateXmlTextWriterAddRSSItem(XmlTextWriterwriter,stringsItemTitle,stringsItemLink,stringsItemDescription,boolbDescAsCDATA)

{

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();

returnwriter;

}

privateXmlTextWriterWriteRSSClosing(XmlTextWriterwriter)

{

writer.WriteEndElement();

writer.WriteEndElement();

writer.WriteEndDocument();

returnwriter;

}

}

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新asp.net教程学习
    热门asp.net教程学习
    编程开发子分类