asp.net(c#) RSS功能實現(xiàn)代碼
更新時間:2008年11月25日 10:11:35 作者:
這兩天一邊從網(wǎng)上找資料,自己再測試,終于完成本站的RSS功能了!先自我恭喜下!!
可能還有很多未完善,但終歸可以使用了,以后再慢慢改進(jìn)!!
以下是我RSS界面的后臺代碼,給需要的朋友提供下我的經(jīng)驗:
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)驗:
復(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實現(xiàn)在線翻譯,英文轉(zhuǎn)中文
- C#獲取真實IP地址實現(xiàn)方法
- ASP.net(c#)打造24小時天氣預(yù)報及實時天氣
- c# AJAX實踐VS2005 + RSSToolKit 開發(fā)你自己的RSS在線閱讀器
- C#中 城市線路圖的純算法以及附帶求極權(quán)值
- C# 根據(jù)ip獲取城市等相關(guān)信息
- C#實現(xiàn)解析百度天氣數(shù)據(jù),Rss解析百度新聞以及根據(jù)IP獲取所在城市的方法
相關(guān)文章
asp.net core2.2多用戶驗證與授權(quán)示例詳解
這篇文章主要給大家介紹了關(guān)于asp.net core2.2多用戶驗證與授權(quán)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01ASP.NET用SignalR建立瀏覽器和服務(wù)器的持久連接詳解
這篇文章主要給大家介紹了ASP.NET用SignalR如何建立瀏覽器和服務(wù)器的持久連接,文章先給大家簡單介紹了配置環(huán)境,而后通過實戰(zhàn)來給大家詳細(xì)的介紹了實現(xiàn)的過程,文中通過一步步的步驟介紹的很詳細(xì),感興趣的朋友們可以參考借鑒,下面來一起看看吧。2016-12-12Asp.net 文件上傳類(取得文件后綴名,保存文件,加入文字水印)
Asp.net 取得文件后綴名,保存文件,加入文字水印的代碼類2008-11-11asp.net下Linq To Sql注意事項小結(jié)
對于Linq 連接數(shù)據(jù)庫進(jìn)行操作時需注意的問題2008-10-10.Net?Core應(yīng)用增強型跨平臺串口類庫CustomSerialPort()詳解
本文詳細(xì)講解了.Net?Core應(yīng)用增強型跨平臺串口類庫CustomSerialPort(),文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01