下面是我总结的几种RSS的技术实现代码:
1.在线生成RSS聚合页。
(1)创建Rss.aspx
<%@ Page language="c#" Codebehind="Rss.aspx.cs" AutoEventWireup="false" Inherits="LiTianPing.Rss" %>
只留下这一行,其余的都删掉。
(2)后台代码实现方式1;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>RSSDemo</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);
}
///
···
···