目前的网站项目里面有一块资讯的栏目,这一次项目组决定用RSS的方式发布,与国际接轨哈。呵呵~
那么我们的新闻资讯需要生成一些频道的RSS文件,还是祭起我们的老伙计:ASP.NET,让它去动态生成这个中规中矩的XML文件吧!
首先我们可得完成RSS的生成类,Channel(频道) 和 Item(条目),在这里只是按RSS的规则添加了一些必要的节点项目。……
目前的网站项目里面有一块资讯的栏目,这一次项目组决定用RSS的方式发布,与国际接轨哈。呵呵~
那么我们的新闻资讯需要生成一些频道的RSS文件,还是祭起我们的老伙计:ASP.NET,让它去动态生成这个中规中矩的XML文件吧!
首先我们可得完成RSS的生成类,Channel(频道) 和 Item(条目),在这里只是按RSS的规则添加了一些必要的节点项目。
类图
ITEM类:很简单,定义了一个条目信息常用的一些元素
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
public class Item
{
字段#region 字段
/**//// <summary>
/// 标题
/// </summary>
private string title;
/**//// <summary>
/// 链接
/// </summary>
private string link;
/**//// <summary>
/// 描述
/// </summary>
private string description;
/**//// <summary>
/// 发布日期
/// </summary>
private string pubdate;
/**//// <summary>
/// 来源
/// </summary>
private string source;
/**//// <summary>
/// 作者
/// </summary>
private string author;
#endregion
属性#region 属性
/**//// <summary>
/// 标题
/// </summary>
public string Title
{
get
{
return this.title;
}
set
{
this.title = value;
}
}
/**//// <summary>
/// 链接
/// </summary>
public string Link
{
get
{
return this.link;
}
set
{
this.link = value;
}
}
/**//// <summary>
/// 描述
/// </summary>
public string Description
{
get
{
return this.description;
}
set
{
this.description = value;
}
}
/**//// <summary>
/// 发布日期
/// </summary>
public string Pubdate
{
get
{
return this.pubdate;
}
set
{
this.pubdate = value;
}
}
/**//// <summary>
/// 来源
/// </summary>
public string Source
{
get
{
return this.source;
}
set
{
this.source = value;
}
}
/**//// <summary>
/// 来源
/// </summary>
public string Author
{
get
{
return this.author;
}
set
{
this.author = value;
}
}
#endregion
构造函数#region 构造函数
/**//// <summary>
/// 资讯条目
/// </summary>
public Item()
{
}
/**//// <summary>
/// 资讯条目
/// </summary>
/// <param name="Title">标题</param>
/// <param name="Link">链接</param>
/// <param name="Description">描述</param>
public Item(string Title, string Link, string Description)
{
this.title = Title;
this.link = Link;
this.description = Description;
}
#endregion
}
CHANNEL类:常用的频道信息,以及一个条目组,对条目组的添加方法,以及把CHANNEL导出XML的一些方法。组成XML文件时,没有传统意义上的XmlDocument的操作方式,感觉麻烦,不若直接按格式凑文本来的快。^_*
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.IO;
using System.Text;
using System.Xml;
public class Channel
{
字段#region 字段
/**//// <summary>
/// 标题
/// </summary>
private string title;
/**//// <summary>
/// 链接
/// </summary>
private string link;
/**//// <summary>
/// 描述
/// </summary>
private string description;
/**//// <summary>
/// 条目组
/// </summary>
private Item[] items;
#endregion
属性#region 属性
/**//// <summary>
/// 描述
/// </summary>
public string Description
{
get
{
return this.description;
}
set
{
this.description = value;
}
}
/**//// <summary>
/// 条目组
/// </summary>
public Item[] Items
{
get
{
return this.items;
}
}
/**//// <summary>
/// 标题
/// </summary>
public string Title
{
get
{
return this.title;
}
set
{
this.title = value;
}
}
/**//// <summary>
/// 链接
/// </summary>
public string Link
{
get
{
return this.link;
}
set
{
this.link = value;
}
}
#endregion
构造函数#region 构造函数
/**//// <summary>
/// 资讯频道
/// </summary>
public Channel()
{
}
#endregion
方法#region 方法
/**//// <summary>
/// 添加条目到频道
/// </summary>
public void AddItem( Item item )
{
if (this.items == null)
{
this.items = new Item[1];
this.items[0] = item;
}
else
{
Item[] itemTemp = new Item[this.items.Length];
itemTemp = this.items;
this.items = new Item[this.items.Length + 1];
for ( int i = 0; i < itemTemp.Length; i ++ )
{
this.items[i] = itemTemp[i];
}
this.items[this.items.Length-1] = item;
}
}
/**//// <summary>
/// 获取频道的RSS文件的内容
/// </summary>
public string GetRSS()
{
StringBuilder strRtn = new StringBuilder();
string strLine = string.Empty;
strLine = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
strRtn.Append(strLine);
strRtn.Append("<rss version=\"2.0\">");
strRtn.Append("<channel>");
strRtn.Append("<title>" + this.title + "</title>");
strRtn.Append("<link>" + this.link + "</link>");
if (!string.IsNullOrEmpty(this.description))
{
strLine = "<description>" + this.description + "</description>";
strRtn.Append(strLine);
}
foreach (Item newsItem in this.items)
{
strRtn.Append("<item>");
strRtn.Append("<title><![CDATA[" + newsItem.Title + " ]]></title>");
strRtn.Append("<link><![CDATA[" + newsItem.Link + " ]]></link>");
if (!string.IsNullOrEmpty(newsItem.Description))
{
strLine = "<description><![CDATA[" + newsItem.Description + " ]]></description>";
strRtn.Append(strLine);
}
if (!string.IsNullOrEmpty(newsItem.Pubdate))
{
strLine = "<pubDate><![CDATA[" + newsItem.Pubdate + " ]]></pubDate>";
strRtn.Append(strLine);
}
if (!string.IsNullOrEmpty(newsItem.Source))
{
strLine = "<source><![CDATA[" + newsItem.Source + " ]]></source>";
strRtn.Append(strLine);
}
if (!string.IsNullOrEmpty(newsItem.Author))
{
strLine = "<author><![CDATA[" + newsItem.Author + " ]]></author>";
strRtn.Append(strLine);
}
strRtn.Append("</item>");
}
strRtn.Append("</channel>");
strRtn.Append("</rss>");
return strRtn.ToString();
}
/**//// <summary>
/// 获取频道的RSS文件
/// </summary>
public void GetRSSFile(string fileName)
{
StreamWriter sw = new StreamWriter(fileName,false);
sw.WriteLine(this.GetRSS());
sw.Close();
}
/**//// <summary>
/// 获取频道的RSS XMLDocument
/// </summary>
public XmlDocument GetRSSDocument()
{
XmlDocument xmlRtn = new XmlDocument();
xmlRtn.LoadXml(this.GetRSS());
return xmlRtn;
}
#endregion
}
类做好了,接下来完成RSS页的代码构建。前台不用管它,需要做的是它的cs程序:
RSS.aspx .cs: 从数据库获取频道和条目信息,组成RSS文件。
访问时,可加上连接字符串,改变频道,例如:
http://localhost/xmlDataSet/Rss.aspx?channel=00001
http://localhost/xmlDataSet/Rss.aspx?channel=00002
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Data.OleDb;
public partial class Rss : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//made by hekui 2007-01-27
根据查询字符串,判断查看的频道#region 根据查询字符串,判断查看的频道
string strChannel_ID;
if (string.IsNullOrEmpty(Request["channel"]))
{
strChannel_ID = "00001";
}
else
{
strChannel_ID = Request["channel"];
}
#endregion
构造频道信息#region 构造频道信息
DataTable dtChannel = getTable("select TITLE,LINK,DESCRIPTION from CHANNEL where CHANNEL_ID = '" + strChannel_ID + "'");
Channel channel = new Channel();
channel.Title = dtChannel.Rows[0]["TITLE"].ToString();
channel.Link = dtChannel.Rows[0]["LINK"].ToString();
channel.Description = dtChannel.Rows[0]["DESCRIPTION"].ToString();
#endregion
构造条目信息#region 构造条目信息
DataTable dtItem = getTable("select TITLE,LINK,DESCRIPTION,PUBDATE,SOURCE,AUTHOR from ITEM where CHANNEL_ID = '" + strChannel_ID + "'");
foreach (DataRow dr in dtItem.Rows)
{
Item item = new Item();
item.Title = dr["TITLE"].ToString();
item.Link = dr["LINK"].ToString();
item.Description = dr["DESCRIPTION"].ToString();
item.Pubdate = dr["PUBDATE"].ToString();
item.Source = dr["SOURCE"].ToString();
item.Author = dr["AUTHOR"].ToString();
channel.AddItem(item);
}
#endregion
RSS内容导出#region RSS内容导出
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "text/xml;charset='utf-8'";
Response.Write(channel.GetRSS());
Response.End();
#endregion
}
获取数据库链接#region 获取数据库链接
private OleDbConnection getConn()
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("news.mdb") + ";Persist Security Info=False";
OleDbConnection conn = new OleDbConnection(strConn);
return conn;
}
#endregion
根据SQL获取DataTable#region 根据SQL获取DataTable
private DataTable getTable(string sqlString)
{
OleDbDataAdapter da = new OleDbDataAdapter(sqlString, getConn());
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
#endregion
}
示例程序:/Files/heekui/xmlDataSet.rar
示例程序里还有一个页面:一个简易的RSS阅读器,代码处理没按套路出牌,直接DataSet装载RSS源的。数据库玩惯了,Table的操作习惯些。