引用
如何在.net动态网站中实现RSS呢?主要思想是编写一个能够自动按照RSS格式生成xml文档的通用类。具体步骤如下:
步骤一:创建RSS通用类
C#代码
using System;
using System.IO;
using System.Data;
using System.Xml;
using
System.Collections.Generic;
using System.Text;
public
class Rss {
public Stream OutputStream;
public string
RssTitle;
public string PublisherUrl;
public string
Description;
public string Copyright;
public string
Generator;
public DataSet ItemSource;
public string
ItemTitleField;
public string ItemUrlField;
public string
ItemDescriptionField;
public string ItemPublicationDateField;
public string ItemAuthor;
public static void
PublishRss(Rss r){
//创建了 System.Xml.XmlTextWriter类的实例,这个类是撰写XML文档的快速方式
XmlTextWriter writer = new XmlTextWriter(r.OutputStream,
Encoding.UTF8);
//这个方法写入XML 1.0版本的声明,即<?xml version="1.0"
encoding="utf-8"?>
writer.WriteStartDocument();
//WriteStartElement这个方法写入指定标签的起始标记。
writer.WriteStartElement("rss");
//这个方法为当前打开的标签写入属性。
writer.WriteAttributeString("version",
"2.0");
writer.WriteStartElement("channel");
//这个方法写入一个起始标记和一个结束标记
writer.WriteElementString("title", r.RssTitle);
writer.WriteElementString("link", r.PublisherUrl);
writer.WriteElementString("description", r.Description);
writer.WriteElementString("copyright", r.Copyright);
writer.WriteElementString("generator", r.Generator);
foreach
(DataRow row in r.ItemSource.Tables[0].Rows) {
writer.WriteStartElement("item");
writer.WriteElementString("author",
row[r.ItemAuthor].ToString());
writer.WriteElementString("title",
row[r.ItemTitleField].ToString());
writer.WriteElementString("link",
row[r.ItemUrlField].ToString());
writer.WriteElementString("description",
row[r.ItemDescriptionField].ToString());
writer.WriteElementString("pubDate",
Convert.ToDateTime(row[r.ItemPublicationDateField]).ToString("dd MMM yyyy
hh:mm:00 "));
writer.WriteEndElement();
}
//写入channel标记的结束标记
writer.WriteEndElement();
//写入RSS标记的结束标记
writer.WriteEndElement();
//将所有缓存的output到目的位置
writer.Flush();
}
}
using System;
using
System.IO;
using System.Data;
using System.Xml;
using
System.Collections.Generic;
using System.Text;
public class Rss {
public Stream OutputStream;
public string RssTitle;
public string
PublisherUrl;
public string Description;
public string Copyright;
public string Generator;
public DataSet ItemSource;
public string
ItemTitleField;
public string ItemUrlField;
public string
ItemDescriptionField;
public string ItemPublicationDateField;
public
string ItemAuthor;
public static void PublishRss(Rss r){
//创建了
System.Xml.XmlTextWriter类的实例,这个类是撰写XML文档的快速方式
XmlTextWriter writer = new
XmlTextWriter(r.OutputStream, Encoding.UTF8);
//这个方法写入XML
1.0版本的声明,即<?xml version="1.0" encoding="utf-8"?>
writer.WriteStartDocument();
//WriteStartElement这个方法写入指定标签的起始标记。
writer.WriteStartElement("rss");
//这个方法为当前打开的标签写入属性。
writer.WriteAttributeString("version", "2.0");
writer.WriteStartElement("channel");
//这个方法写入一个起始标记和一个结束标记
writer.WriteElementString("title", r.RssTitle);
writer.WriteElementString("link", r.PublisherUrl);
writer.WriteElementString("description", r.Description);
writer.WriteElementString("copyright", r.Copyright);
writer.WriteElementString("generator", r.Generator);
foreach (DataRow row
in r.ItemSource.Tables[0].Rows) {
writer.WriteStartElement("item");
writer.WriteElementString("author", row[r.ItemAuthor].ToString());
writer.WriteElementString("title", row[r.ItemTitleField].ToString());
writer.WriteElementString("link", row[r.ItemUrlField].ToString());
writer.WriteElementString("description",
row[r.ItemDescriptionField].ToString());
writer.WriteElementString("pubDate",
Convert.ToDateTime(row[r.ItemPublicationDateField]).ToString("dd MMM yyyy
hh:mm:00 "));
writer.WriteEndElement();
}
//写入channel标记的结束标记
writer.WriteEndElement();
//写入RSS标记的结束标记
writer.WriteEndElement();
//将所有缓存的output到目的位置
writer.Flush();
}
}
Note:这里pubDate的日期格式很重要,当你按上面的代码对pubDate进行格式转换的时候, 假如数据库中是 2007-9-14
9:58,那么在英文操作系统下,会转换成 “14 Sep 2007 9:58”, 这个是没有问题的。但在中文操作系统下,就变成了 “14 九月 2007
9:58”。导致的结果就是在IE7中点开rss源的 时候,发现日期没有显示。如果你不进行数据格式转换,简单的使用一个
ToString(),结果仍是如此。这里写了一个格式转换代码:
C#代码
public static string
GetRssDate(Object date) {
DateTime rssDate = Convert.ToDateTime(date);
string[] monthName = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
StringBuilder sb = new
StringBuilder();
sb.Append(rssDate.Day);
sb.Append(" ");
sb.Append(monthName[Convert.ToInt32(rssDate.Month) - 1]);
sb.Append(" ");
sb.Append(rssDate.Year);
sb.Append(" ");
sb.Append(rssDate.ToLongTimeString());
return
sb.ToString();
}
public static string GetRssDate(Object
date) {
DateTime rssDate = Convert.ToDateTime(date);
string[] monthName =
{ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov",
"Dec" };
StringBuilder sb = new StringBuilder();
sb.Append(rssDate.Day);
sb.Append(" ");
sb.Append(monthName[Convert.ToInt32(rssDate.Month) - 1]);
sb.Append(" ");
sb.Append(rssDate.Year);
sb.Append(" ");
sb.Append(rssDate.ToLongTimeString());
return sb.ToString();
}
步骤二:创建web窗体(RSS.aspx)
1.在RSS.aspx中删出除第一行外的所有代码
2.在RSS.aspx中实例化RSS类
C#代码
protected void Page_Load(object sender,
EventArgs e){
//GetDataSet从数据库筛选出RSS源所需的相关内容如:aritcle,Url,description,pubDate.
DataSet ds = GetDataSet();
Rss rss = new Rss();
rss.OutputStream = Response.OutputStream;
rss.RssTitle =
"DotNetBips.com Latest Articles";
rss.PublisherUrl = Request.Url.Host;
rss.Description = "DotNetBips.com - Applying .NET";
rss.Copyright = "Copyright (C) DotNetBips.com.";
rss.Generator =
"DotNetBips.com RSS Generator";
rss.ItemSource = ds;
rss.ItemTitleField = "Title";
rss.ItemDescriptionField =
"Description";
rss.ItemPublicationDateField = "Pubdate";
rss.ItemUrlField = "Url";
rss.ItemAuthor = "Author";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "text/xml";
//调用Rss类中的静态方法
Rss.PublishRss(rss);
Response.End();
}
protected void Page_Load(object sender, EventArgs e){
//GetDataSet从数据库筛选出RSS源所需的相关内容如:aritcle,Url,description,pubDate.
DataSet ds =
GetDataSet();
Rss rss = new Rss();
rss.OutputStream =
Response.OutputStream;
rss.RssTitle = "DotNetBips.com Latest Articles";
rss.PublisherUrl = Request.Url.Host;
rss.Description = "DotNetBips.com -
Applying .NET";
rss.Copyright = "Copyright (C) DotNetBips.com.";
rss.Generator = "DotNetBips.com RSS Generator";
rss.ItemSource = ds;
rss.ItemTitleField = "Title";
rss.ItemDescriptionField = "Description";
rss.ItemPublicationDateField = "Pubdate";
rss.ItemUrlField = "Url";
rss.ItemAuthor = "Author";
Response.ContentEncoding =
System.Text.Encoding.UTF8;
Response.ContentType = "text/xml";
//调用Rss类中的静态方法
Rss.PublishRss(rss);
Response.End();
}
步骤三:链接RSS.aspx
在如上两步操作后,RSS的实现已经完成了99%,剩下的就是在需要放置RSS源的页面中(如default.aspx)放置如下代码:
C#代码
1. <link rel="alternate" type="application/rss+xml" title="Your Web Site’s
RSS Feed Title " href="rss.aspx" />
相关文章
- 07-13js中自己实现bind方法及详解
- 07-13关于.NET中迭代器的实现以及集合扩展方法的理解
- 07-13SpringAOP在一个实现类中定义自身的方法, 无法调用, 只能调用实现接口的方法
- 07-13ML之Validation:机器学习中模型验证方法的简介、代码实现、案例应用之详细攻略
- 07-13图像加密中测试混沌系统性能的 0-1测试/0-1 Test方法与MATLAB实现代码
- 07-13在.net中实现在textbox中按ctrl+enter进行数据的提交
- 07-13python-在PyObjc和Cocoa中实现NSText委托方法
- 07-13自己动手实现scikit库中的fit和transform方法
- 07-13Spring / AOP:在数据库中实现活动日志的最佳方法
- 07-13实现一个方法,找出数组中第k大的和第m大的数字相加之和