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. 我的源码
- 最新文章
-
PyQt4 在 Windows 上的安装 Roy Chen的日志 网易博客
将python3.1+pyqt4打包成exe
python实现层次聚类
fastcluster: Fast hierarchical clustering routines for R and Python
Apache Jena
What is pyjs?
GUI Programming with Python: QT Edition
PyQt4 在 Windows 上的安装
利用Python和Beautiful Soup抓取网页内容
idea
- 热门文章
-
GUI Programming with Python: QT Edition
idea
Installing Poppler for PyQt4 on Windows
快排为什么那样快(转)
用Scipy实现Kmeans聚类算法
LINQToSQL中如何更好的手动设置导航字段,并返回实名类型而不是匿名类型
架构那些事~MVC系统架构中哪些应该进行抽象
基础才是重中之重~用好“逻辑与”和“逻辑或”,可以让你的程序更优美!
使用MVC中的HtmlHelper对象的ActionLink方法,实现一个Javascript:void(0)效果,去调用一个JS方法
LINQ TO SQL中还是用传统的连接串方式建立DbContext更好些
Copyright © 2020-2023
润新知