asp.net(c#) RSS功能實(shí)現(xiàn)代碼
更新時(shí)間:2008年11月25日 10:11:35 作者:
這兩天一邊從網(wǎng)上找資料,自己再測試,終于完成本站的RSS功能了!先自我恭喜下!!
可能還有很多未完善,但終歸可以使用了,以后再慢慢改進(jìn)!!
以下是我RSS界面的后臺代碼,給需要的朋友提供下我的經(jīng)驗(yàn):
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界面的后臺代碼,給需要的朋友提供下我的經(jīng)驗(yàn):
復(fù)制代碼 代碼如下:
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;
}
}
您可能感興趣的文章:
- c#封裝百度web服務(wù)geocoding api 、百度坐標(biāo)轉(zhuǎn)換示例
- asp.net c# 調(diào)用百度pai實(shí)現(xiàn)在線翻譯,英文轉(zhuǎn)中文
- C#獲取真實(shí)IP地址實(shí)現(xiàn)方法
- ASP.net(c#)打造24小時(shí)天氣預(yù)報(bào)及實(shí)時(shí)天氣
- c# AJAX實(shí)踐VS2005 + RSSToolKit 開發(fā)你自己的RSS在線閱讀器
- C#中 城市線路圖的純算法以及附帶求極權(quán)值
- C# 根據(jù)ip獲取城市等相關(guān)信息
- C#實(shí)現(xiàn)解析百度天氣數(shù)據(jù),Rss解析百度新聞以及根據(jù)IP獲取所在城市的方法
相關(guān)文章
ASP.Net Post方式獲取數(shù)據(jù)流的一種簡單寫法
這篇文章主要介紹了ASP.Net Post方式獲取數(shù)據(jù)流的一種簡單寫法,本文直接給出代碼實(shí)例,需要的朋友可以參考下2015-05-05
.Net學(xué)習(xí)筆記之Layui多圖片上傳功能
這篇文章主要給大家介紹了關(guān)于.Net學(xué)習(xí)筆記之Layui多圖片上傳功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用.Net具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
asp.net(C#)函數(shù)對象參數(shù)傳遞的問題
我們知道在.net里class是引用類型,在函數(shù)參數(shù)表中的對象傳遞的都是對象的引用,所以在函數(shù)體內(nèi)對其對象參數(shù)的修改會(huì)影響函數(shù)外對應(yīng)的對象本身,例如下面的程序.2009-12-12
asp.net無法加載oci.dll等錯(cuò)誤的解決方法
.net在windows2003下訪問oracle9i提示“無法加載oci.dll”或"無法在dll oci.dll中找到名為ocienvcreate的入口點(diǎn) "的修復(fù)方法2013-10-10
ASP.NET Core實(shí)現(xiàn)自動(dòng)依賴注入
這篇文章主要介紹了ASP.NET Core實(shí)現(xiàn)自動(dòng)依賴注入的示例,幫助大家更好的理解和學(xué)習(xí)使用.net技術(shù),感興趣的朋友可以了解下2021-04-04

