RSS 订阅功能的实现
2010-03-02 18:39 by clound, 78 visits, 收藏, 编辑很多站点多会用到RSS订阅功能,今天写了个,留个笔记。
我们可以用.aspx,.ashx文件构建xml文件,输出xml 可以通过 XmlTextWriter实例化来输出,也可以直接输出。
1.利用XmlTextWriter输出
代码
public String uid = "461";
public String UserInfo = String.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["id"] != null && Request.QueryString["id"].ToString() != "")
{
uid = Request.QueryString["id"].ToString();
}
Response.ContentType = "application/rss+xml";
Response.ContentEncoding = Encoding.UTF8;
XmlTextWriter rsswriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
WriteRssOpening(rsswriter);
WriteRssBody(rsswriter);
WriteRssEnding(rsswriter);
rsswriter.Flush();
Response.End();
}
/// <summary>
/// 信息标题
/// </summary>
/// <param name="rsswriter"></param>
private void WriteRssOpening(XmlTextWriter rsswriter)
{
UserBasicModelInfo info = UserBasic.GetBasicList(uid);
rsswriter.WriteStartElement("rss");
rsswriter.WriteAttributeString("version", "2.0");
rsswriter.WriteStartElement("channel");
rsswriter.WriteElementString("title", info.TrueName+"的官方博客");
rsswriter.WriteElementString("link", Application["Url"] + "/Rss.aspx?id=" + uid + "");
rsswriter.WriteElementString("description", info.TrueName + "的官方博客");
}
/// <summary>
/// 信息整体
/// </summary>
/// <param name="rsswriter"></param>
private void WriteRssBody(XmlTextWriter rsswriter)
{
DataTable GetDate = wdxt_Common.GetSomeData.GetDataNum("VW_NEWLOG", "*", "LOGSEECOMPETENCE=1 and userinfoid=" + uid + "", "addtime desc ", 100).Tables[0];
foreach (DataRow rssitem in GetDate.Rows)
{
rsswriter.WriteStartElement("item");
rsswriter.WriteElementString("title", rssitem["logtitle"].ToString());
rsswriter.WriteElementString("author", rssitem["TRUENAME"].ToString());
rsswriter.WriteElementString("link", ""+Application["Url"]+"/Log/show_" + rssitem["logid"].ToString() + "_6");
rsswriter.WriteElementString("description", rssitem["logcontent"].ToString());
rsswriter.WriteElementString("pubDate", rssitem["addtime"].ToString());
rsswriter.WriteEndElement();
}
}
private void WriteRssEnding(XmlTextWriter rsswriter)
{
rsswriter.WriteEndElement();
rsswriter.WriteEndElement();
}
public String UserInfo = String.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["id"] != null && Request.QueryString["id"].ToString() != "")
{
uid = Request.QueryString["id"].ToString();
}
Response.ContentType = "application/rss+xml";
Response.ContentEncoding = Encoding.UTF8;
XmlTextWriter rsswriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
WriteRssOpening(rsswriter);
WriteRssBody(rsswriter);
WriteRssEnding(rsswriter);
rsswriter.Flush();
Response.End();
}
/// <summary>
/// 信息标题
/// </summary>
/// <param name="rsswriter"></param>
private void WriteRssOpening(XmlTextWriter rsswriter)
{
UserBasicModelInfo info = UserBasic.GetBasicList(uid);
rsswriter.WriteStartElement("rss");
rsswriter.WriteAttributeString("version", "2.0");
rsswriter.WriteStartElement("channel");
rsswriter.WriteElementString("title", info.TrueName+"的官方博客");
rsswriter.WriteElementString("link", Application["Url"] + "/Rss.aspx?id=" + uid + "");
rsswriter.WriteElementString("description", info.TrueName + "的官方博客");
}
/// <summary>
/// 信息整体
/// </summary>
/// <param name="rsswriter"></param>
private void WriteRssBody(XmlTextWriter rsswriter)
{
DataTable GetDate = wdxt_Common.GetSomeData.GetDataNum("VW_NEWLOG", "*", "LOGSEECOMPETENCE=1 and userinfoid=" + uid + "", "addtime desc ", 100).Tables[0];
foreach (DataRow rssitem in GetDate.Rows)
{
rsswriter.WriteStartElement("item");
rsswriter.WriteElementString("title", rssitem["logtitle"].ToString());
rsswriter.WriteElementString("author", rssitem["TRUENAME"].ToString());
rsswriter.WriteElementString("link", ""+Application["Url"]+"/Log/show_" + rssitem["logid"].ToString() + "_6");
rsswriter.WriteElementString("description", rssitem["logcontent"].ToString());
rsswriter.WriteElementString("pubDate", rssitem["addtime"].ToString());
rsswriter.WriteEndElement();
}
}
private void WriteRssEnding(XmlTextWriter rsswriter)
{
rsswriter.WriteEndElement();
rsswriter.WriteEndElement();
}
2. 直接输出XML
代码
protected String uid = String.Empty;
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
uid = HttpContext.Current.Request.QueryString["id"].ToString();
Response.ContentType = "text/xml";
Response.Charset = "GB2312";
Response.Write(GetRSS());
}
#region
/// <summary>
/// 取得聚合文章
/// </summary>
public string GetRSS()
{
UserBasicModelInfo info = UserBasic.GetBasicList(uid);
DataSet ds = wdxt_Common.GetSomeData.GetDataNum("VW_NEWLOG", "*", "LOGSEECOMPETENCE=1 and userinfoid=" + uid + "", "addtime desc ", 100);
String strRSS = "<?xml version=\"1.0\" encoding=\"gb2312\" ?><rss version=\"2.0\">";
strRSS += "<channel>";
strRSS += "<title>" + info.TrueName + "的博客</title>";
strRSS += "<link>" + Application["Url"] + "/Myspace/Space/Rss1.aspx?id=" + uid + "</link>";
strRSS += "<description></description>";
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string Logid = ds.Tables[0].Rows[i]["logid"].ToString();
string LogContent = ds.Tables[0].Rows[i]["LogContent"].ToString();
string Title = ds.Tables[0].Rows[i]["logtitle"].ToString();
String LogAddTime = ds.Tables[0].Rows[i]["Addtime"].ToString();
string truename = ds.Tables[0].Rows[i]["TrueName"].ToString();
strRSS += "<item>";
strRSS += "<title>" + Title + "</title>";
strRSS += "<link>" + Application["Url"] + "/Blog/info/" + Logid + "</link>";
strRSS += "<pubDate>" + LogAddTime + "</pubDate>";
strRSS += "<copyright>" + truename + "</copyright>";
strRSS += "<description><![CDATA[" + LogContent + "]]></description>";
strRSS += "</item>";
}
strRSS += "</channel>";
strRSS += "</rss>";
return strRSS;
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
uid = HttpContext.Current.Request.QueryString["id"].ToString();
Response.ContentType = "text/xml";
Response.Charset = "GB2312";
Response.Write(GetRSS());
}
#region
/// <summary>
/// 取得聚合文章
/// </summary>
public string GetRSS()
{
UserBasicModelInfo info = UserBasic.GetBasicList(uid);
DataSet ds = wdxt_Common.GetSomeData.GetDataNum("VW_NEWLOG", "*", "LOGSEECOMPETENCE=1 and userinfoid=" + uid + "", "addtime desc ", 100);
String strRSS = "<?xml version=\"1.0\" encoding=\"gb2312\" ?><rss version=\"2.0\">";
strRSS += "<channel>";
strRSS += "<title>" + info.TrueName + "的博客</title>";
strRSS += "<link>" + Application["Url"] + "/Myspace/Space/Rss1.aspx?id=" + uid + "</link>";
strRSS += "<description></description>";
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string Logid = ds.Tables[0].Rows[i]["logid"].ToString();
string LogContent = ds.Tables[0].Rows[i]["LogContent"].ToString();
string Title = ds.Tables[0].Rows[i]["logtitle"].ToString();
String LogAddTime = ds.Tables[0].Rows[i]["Addtime"].ToString();
string truename = ds.Tables[0].Rows[i]["TrueName"].ToString();
strRSS += "<item>";
strRSS += "<title>" + Title + "</title>";
strRSS += "<link>" + Application["Url"] + "/Blog/info/" + Logid + "</link>";
strRSS += "<pubDate>" + LogAddTime + "</pubDate>";
strRSS += "<copyright>" + truename + "</copyright>";
strRSS += "<description><![CDATA[" + LogContent + "]]></description>";
strRSS += "</item>";
}
strRSS += "</channel>";
strRSS += "</rss>";
return strRSS;
}
#endregion
参考:1. 微软的CSASPNETRssFeeds
2. 我的源码
微信支持的Authorization code授权模式(公众号开发)(开放平台资料中心中的代公众号发起网页授权)
HttpClient类详解
PostgreSQL按年月日分组(关键词:extract time as Year/Month/Day)
sql中的(case when then else end )的用法(相当于java中的if else)
模板题 Problem I Link Cut Tree
BZOJ2555 SubString
poj3264(简单线段树)
poj1011(DFS+剪枝)
poj1042(贪心+枚举)
- 最新文章
-
ABAP中Conversion Routine示例
SAP中获取当前用户相关信息的两个函数
访问SAP的Domain的Value Range
SAP RFC通信模式
在选择屏幕的标准应用工具条上增加自定义按钮
abap 选择屏幕事件AT SELECTION-SCREEN
模拟Select-Options对象实现多项数据输入功能
pinmap 和 pin allocation
USART 串口
1.RCC of STM32F103
- 热门文章
-
MDK 添加芯片支持 以STM32F103ZE为例
MDK 虚拟串口 *** error 30: undefined name of virtual register
STM32F4X 关于MDK上虚拟串口调试
qfc 问题汇总(TO BE CONTINUED)
Ubuntu下搭建Pixhawk开发环境
vs2017 Visual Studio 离线安装方法
sql中使一个字段升序,一个字段降序
什么是跨域及怎么解决跨域问题?
JQuery中html、append、appendTo、after、insertAfter、before、insertBefore、empty、remove的使用
Node.js安装及环境配置
Copyright © 2011-2022 走看看