.net中實現RSS方法

引用
如何在.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" /> web

相關文章
相關標籤/搜索