|
 |
栏目导栏 |
|
| |
|
|
|
|
 |
资料搜索 |
|
| |
|
|
|
|
 |
热门文章 |
|
| |
|
|
|
|
 |
最新文章 |
|
| |
|
|
|
| |
| |
|
|
|
|
关于Rss FKULinux联盟 FKULinux联盟 RSS是共享内容的一种简易方式,也称聚合.通常被用于新闻和Blog。网站提供RSS输出,有利于让用户发现网站内容的更新。网站用户可以借助于支持RSS的新闻聚合工具软件或者在线网站(如BLOGLINES),订阅网站的内容并在不打开网站页面的情况下阅读内容。 FKULinux联盟 FKULinux联盟 RSS是基于XML的一种形式,具体规范我就不说了,下面是如何使用ASP.NET实现 FKULinux联盟 FKULinux联盟 FKULinux联盟 通知浏览器网站提供了RSS输出: FKULinux联盟 把下面的这句放在博客或这站点的<head>标签中 FKULinux联盟 FKULinux联盟 <link id="RSSLink" title="RSS" type="application/rss+xml" rel="alternate" href="http://www.cnblogs.com/lbk/rss.aspx" /> FKULinux联盟 FKULinux联盟 创建RSS.ASPX文件 FKULinux联盟 只留下面这一行,其余的都删掉。 FKULinux联盟 FKULinux联盟 <%@ Page language="c#" Codebehind="Rss.aspx.cs" AutoEventWireup="false" Inherits="Test.Rss" %> FKULinux联盟 FKULinux联盟 后台代码;Rss.aspx.cs FKULinux联盟 这里XML文件内容的输出是采用直接输出字符串的形式,在我看来前者比较简单,你也可以使用XmlTextWriter来实现。 FKULinux联盟 FKULinux联盟 private void Page_Load(object sender, System.EventArgs e) FKULinux联盟 { FKULinux联盟 Response.ContentType="text/xml"; FKULinux联盟 Response.Charset = "UTF-8"; FKULinux联盟 Response.Write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); FKULinux联盟 Response.Write("<rss version=\"2.0\"><channel>"); FKULinux联盟 FKULinux联盟 Response.Write(GetUserRSS()); FKULinux联盟 GetItemsRss(); FKULinux联盟 FKULinux联盟 Response.Write("</channel></rss>"); FKULinux联盟 } FKULinux联盟 //此RSS的一些信息 FKULinux联盟 private string GetUserRSS(NewTaokee.Model.Users user) FKULinux联盟 { FKULinux联盟 StringBuilder strRss = new StringBuilder(); FKULinux联盟 strRss.Append("<title>lbk</title>"); FKULinux联盟 strRss.Append("<link>lbk.cnblogs.com</link> "); FKULinux联盟 strRss.Append("<description>呵呵,就是我</description> "); FKULinux联盟 strRss.Append("<language>zh-cn</language>"); FKULinux联盟 strRss.Append("<generator>www.xiaoqian.cn </generator>"); FKULinux联盟 return strRss.ToString(); FKULinux联盟 } FKULinux联盟 //循环读取RSS中的每个ITEM的内容 FKULinux联盟 private string getRss(IList<Model.Info> listInfo) FKULinux联盟 { FKULinux联盟 StringBuilder strRss = new StringBuilder(); FKULinux联盟 foreach (Model.Info var in listInfo) FKULinux联盟 { FKULinux联盟 string nickname = DAL.Users.instance.GetModelBasic(var.Author).NickName; FKULinux联盟 string content = Function.ReplaceHtml(var.Content); FKULinux联盟 content = Function.ReplaceXml(content); FKULinux联盟 content = (content.Length > 300) ? content.Substring(0, 300) + "" : content; FKULinux联盟 string link = Function.getConfig("MyUrl") + nickname + "/blog/item/" + var.id; FKULinux联盟 FKULinux联盟 strRss.Append("<item>"); FKULinux联盟 strRss.Append("<title>" + Function.ReplaceXml(var.Title) + "</title>"); FKULinux联盟 strRss.Append("<author>" + nickname + "</author>"); FKULinux联盟 strRss.Append("<description><![CDATA[" + content + "<p><a href='" + link + "'>查看全文</a>" + "]]></description>"); FKULinux联盟 strRss.Append("<pubDate>" + var.Adddate.ToString() + "</pubDate>"); FKULinux联盟 strRss.Append("<link>" + link + "/</link>"); FKULinux联盟 strRss.Append("</item>"); FKULinux联盟 } FKULinux联盟 return strRss.ToString(); FKULinux联盟 } FKULinux联盟 FKULinux联盟 XML中的特殊字符的过滤(否则生成的XML文件会出错): FKULinux联盟 public static string ReplaceXml(string msg) FKULinux联盟 { FKULinux联盟 if (msg != "" && msg != null) FKULinux联盟 { FKULinux联盟 msg = msg.Replace("&", "%26"); FKULinux联盟 msg = msg.Replace(">", ">"); FKULinux联盟 msg = msg.Replace("<", "<"); FKULinux联盟 msg = msg.Replace("\"", """); FKULinux联盟 msg = msg.Replace("'", "'"); FKULinux联盟 } FKULinux联盟 return msg; FKULinux联盟 } FKULinux联盟 另外要注意的是,如果你的RSS输出的链接中又中文字符的话, FKULinux联盟 如: cnblogs.com/你好/rss.aspx, FKULinux联盟 最好能使用Server.UrlEncode("你好"),编码之后才能使用抓虾等订阅
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论 |
|
|
|
|
|