如见web2.0被吵的如火如荼,同时也有很多的评论褒贬不一。有人说web2.0就是“一帮人在看皇帝的新装而已”。也有人说web2.0将是一场变革。无论这些争论有没有价值,但我相信,存在就有它的必然性。也许web2.0更像是一种商业模式,一个观念的变革而非技术。
不管怎样,它来了,你准备好了吗?
说到web2.0,就会提到blog、tag、sns、rss、wiki这些软件应用和xml、ajax等这些新理论和技术实现。
rss毋容置疑就是其重要的应用之一。
那么什么是rss?它到底有什么用呢?
rss是站点与站点之间共享内容的一种简易方式(也称为“聚合内容”),通常被用于新闻和其他按顺序排列的网站,例如blog网站。网站提供rss输出,有利于让用户发现网站内容的更新。网站用户可以在客户端借助于类似新闻资讯阅读器等支持rss的新闻聚合工具软件,在不打开网站内容页面的情况下阅读支持rss输出的网站内容。
rss是基于xml的一种形式。并且所有的rss文件都要遵守万维网联盟(w3c)站点发布的xml 1.0规范。具体格式可以查一下rss 2.0规范,这里就不再重复粘贴了。
下面是我总结的几种rss的技术实现代码:
1.在线生成rss聚合页。
(1)创建rss.aspx
<%@ page language="c#" codebehind="rss.aspx.cs" autoeventwireup="false" inherits="litianping.rss" %>
只留下这一行,其余的都删掉。
(2)后台代码;rss.aspx.cs
private void page_load(object sender, system.eventargs e)
{
response.contenttype="text/xml";
response.write(getrss());
}
/// <summary>
/// 取得聚合文章
/// </summary>
/// <returns></returns>
public string getrss()
{
news t=new news();//自己的业务类
dataset ds=t.getlistbyclass(1);//根据类别得到数据
stringbuilder strcode=new stringbuilder();
strcode.append("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>");
strcode.append("<rss version=2.0 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"");
strcode.append(" xmlns:trackback=\"http://madskills.com/public/xml/rss/module/trackback/\" ");
strcode.append(" xmlns:wfw=\"http://wellformedweb.org/commentapi/\" xmlns:slash=\"http://purl.org/rss/1.0/modules/slash/\">");
strcode.append("<channel>");
strcode.append("<title>李天平rssdemo</title>");
strcode.append("<link>http://"+request.servervariables["server_name"]+"</link> ");
strcode.append("<description>天道酬勤</description> ");
strcode.append("<copyright>copyright 2005</copyright> ");
foreach(datarow row in ds.tables[0].rows)
{
string id=row["id"].tostring();
string title=row["title"].tostring();
string description=row["description"].tostring();
string pubdate=row["pubdate"].tostring();
string classid=row["classid"].tostring();
//string author=row["author"].tostring();
strcode.append("<item>");
strcode.append("<title>"+title+"</title>");
strcode.append("<link>http://"+request.servervariables["server_name"]+"/newsshow.aspx?id="+id+"</link>");
strcode.append("<subject>"+description+"</subject>");
strcode.append("<description><![cdata["+description+"]]></description>");
strcode.append("<pubdate>"+pubdate+"</pubdate>");
strcode.append("<category>"+classid+"</category>");
strcode.append("</item>");
}
strcode.append("</channel>");
strcode.append("</rss>");
return strcode.tostring();
}
(3) xmltextwriter实现方式2;rss.aspx.cs
string xmldoc="rss.xml";
private void page_load(object sender, system.eventargs e)
{
xmldoc=server.mappath(".")+xmldoc;
getrss2();
xmldocument doc= new xmldocument();
doc.load(xmldoc);
response.contenttype = "text/xml";
doc.save(response.output);
}
/// <summary>
/// 取得聚合文章
/// </summary>
/// <returns></returns>
public void getrss2()
{
news t=new news();
dataset ds=t.getlistbyclass(1);
xmltextwriter writer = new xmltextwriter(xmldoc,encoding.utf8);
writer.formatting = formatting.indented;
writer.writestartdocument(true);
writer.writecomment("rss页的实现");
writer.writestartelement("rss");
writer.writeattributestring("version","2.0");
writer.writestartelement("channel");
writer.writestartelement("title");
writer.writestring("李天平rssdemo");
writer.writeendelement();
writer.writestartelement("link");
writer.writestring("http://"+request.servervariables["server_name"]);
writer.writeendelement();
writer.writestartelement("description");
writer.writestring("天道酬勤");
writer.writeendelement();
writer.writestartelement("copyright");
writer.writestring("copyright 2005");
writer.writeendelement();
writer.writestartelement("language");
writer.writestring("zh-cn");
writer.writeendelement();
foreach(datarow row in ds.tables[0].rows)
{
string id=row["id"].tostring();
string title=row["title"].tostring();
string description=row["description"].tostring();
string pubdate=row["pubdate"].tostring();
string classid=row["classid"].tostring();
//string author=row["author"].tostring();
writer.writestartelement("item");
writer.writestartelement("title");
writer.writestring(title);
writer.writeendelement();
writer.writestartelement("link");
writer.writestring("http://"+request.servervariables["server_name"]+"/newsshow.aspx?id="+id) ;
writer.writeendelement();
writer.writestartelement("description");
writer.writecdata(description);
writer.writeendelement();
writer.writestartelement("pubdate");
writer.writestring(pubdate);
writer.writeendelement();
writer.writestartelement("category");
writer.writestring(classid);
writer.writeendelement();
writer.writeendelement();
}
writer.writeendelement();
writer.writeendelement();
writer.flush();
writer.close();
}
2.在线rss阅读获取。
页面代码:
<p style="font-size: 11px">
feed种子:<asp:textbox id="feedurl" runat="server" width="440px"></asp:textbox><br>
显示条数:<asp:textbox id="num" runat="server" width="48px"></asp:textbox></p>
<p style="font-size: 11px">
<asp:button id="getfeed" runat="server" text="获得rss"></asp:button><br>
<br>
<asp:label id="rssfeed" runat="server"></asp:label></p>
后台代码:
/// <summary>
/// 获得要显示的feed种子数据
/// </summary>
/// <param name="rssurl"></param>
/// <param name="shownewscount"></param>
/// <returns></returns>
public string loadrss(string rssurl, int shownewscount)
{
string strrsslist = "";
string strmsg;
try
{
xmldocument objxmldoc = new xmldocument();
objxmldoc.load(rssurl);
xmlnodelist objitems = objxmldoc.getelementsbytagname("item");
if(shownewscount > 30)
shownewscount = 10;
if(shownewscount < 1)
shownewscount = objitems.count;
string title="";
string link="";
int i;
if(objxmldoc.haschildnodes == true)
{
i = 1;
foreach(xmlnode objnode in objitems)
{
if(i<=shownewscount)
{
if(objnode.haschildnodes == true)
{
xmlnodelist objitemschild = objnode.childnodes;
foreach(xmlnode objnodechild in objitemschild)
{
switch(objnodechild.name)
{
case "title":
title = objnodechild.innertext;
break;
case "link":
link = objnodechild.innertext;
break;
}
}
i = i+1;
strrsslist += "<a href=" + link +" target=_blank>" + title + "</a><br>";
}
}
}
}
strmsg = strrsslist;
}
catch
{
strmsg = "rss feed 源数据出错!";
}
return strmsg;
}
//获取按钮,获取指定rss
private void getfeed_click(object sender, system.eventargs e)
{
if(feedurl.text == "")//rss地址
{
rssfeed.text = "信息源不能为空,您可刷新重试或联系管理员!";
return ;
}
rssfeed.text = loadrss(feedurl.text,convert.toint32(num.text)); //获取指定数目
}
3.无刷新动态更新的在线rss阅读获取
<%@ page language="c#" codebehind="xmlhttp.aspx.cs" autoeventwireup="false" inherits="litianping.xmlhttp" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>xmlhttp</title>
<meta name="generator" content="microsoft visual studio .net 7.1">
<meta name="code_language" content="c#">
<meta name="vs_defaultclientscript" content="javascript">
<meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
<style>td { font-size: 9pt}
</style>
<script>
<!--
var odiv
var xh
function getxml()
{
xh =new activexobject("microsoft.xmlhttp");
odiv = document.getelementbyid("rssitem");
odiv.style.display= "";
try
{
//如果是本地改成下面语句直接读取
//xh.open("get","/rss.aspx",false);
xh.open("get","http://ltp.cnblogs.com/rss.aspx",false);
xh.send(null);
loaddata(xh,odiv);
}
catch(e)
{
error(odiv);
}
}
function loaddata(xh,odiv)
{
if (xh.readystate == 4)
{
xml = xh.responsexml;
var i;
var nodes = xml.selectnodes("/rss/channel/item");
var bloglink = xml.selectsinglenode("/rss/channel/link").text;
odiv.innerhtml = "";
var html;
for(i=0;i<nodes.length;i++)
{
html += "<div >";
html += " <div >";
html += " <a class="font1" href=" +nodes[i].selectsinglenode("link").text + " target=_blank>" + nodes[i].selectsinglenode("title").text +"</a><br><br>";
html += " </div>";
html += " <div >";
html += " " +nodes[i].selectsinglenode("description").text;
html += " </div>"
//html += " </div>";
html += "</div><hr>";
}
odiv.innerhtml = html;
}
else
{
error(odiv);
}
}
function error(odiv)
{
odiv.innerhtml = "载入失败";
}
-->
</script>
</head>
<body ms_positioning="gridlayout" onload="window.settimeout(getxml(),200);">
<form id="form1" method="post" runat="server">
<div align=center>
<table cellpadding="0" cellspacing="0" border="0" width="80%">
<tr>
<td>
<div id="rssitem" style="80%">数据载入中...</div>
</td>
</tr>
</table>
</div>
</form>
</body>
</html> (作者:李天平 转载请注明)