不管怎样,它来了,你准备好了吗?
说到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
只留下这一行,其余的都删掉。
(2)后台代码;Rss.aspx.cs
private void Page_Load(object sender, System.EventArgs e)
{
Response.ContentType="text/xml";
Response.Write(GetRSS());
}
///
/// 取得聚合文章
///
///
public string GetRSS()
{
News t=new News();//自己的业务类
DataSet ds=t.GetListByClass(1);//根据类别得到数据
StringBuilder strCode=new StringBuilder();
strCode.Append("");
strCode.Append("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("");
strCode.Append("");
strCode.Append("http://"+Request.ServerVariables["SERVER_NAME"]+" ");
strCode.Append("天道酬勤 ");
strCode.Append("Copyright 2005 ");
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("");
strCode.Append("");
strCode.Append("http://"+Request.ServerVariables["SERVER_NAME"]+"/NewsShow.aspx?ID="+Id+"");
strCode.Append(""+description+"");
strCode.Append("");
strCode.Append(""+pubdate+"");
strCode.Append(""+ClassId+"");
strCode.Append("");
}
strCode.Append("");
strCode.Append("");
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);
}
///
/// 取得聚合文章
///
///
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阅读获取。
页面代码:
Feed种子:
显示条数:
后台代码:
///
/// 获得要显示的Feed种子数据
///
///
///
///
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 += "" + title + "
";
}
}
}
}
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阅读获取
http://schemas.microsoft.com/intellisense/ie5">
td { font-size: 9pt}
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 {
html += "
html += " ";
html += "
html += " " +nodes.selectSingleNode("description").text;
html += "
//html += "
html += "
";
}
oDiv.innerHTML = html;
}
else
{
error(oDiv);
}
}
function error(oDiv)
{
oDiv.innerHTML = "载入失败";
}
-->