.net中实现RSS方法

引用
如何在.net动态网站中实现RSS呢?主要思想是编写一个能够自动按照RSS格式生成xml文档的通用类。具体步骤如下:

步骤一:创建RSS通用类
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(),结果仍是如此。这里写了一个格式转换代码:
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类
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)放置如下代码:
<link rel="alternate" type="application/rss+xml" title="Your Web Site’s RSS Feed Title " href="rss.aspx" />

你可能感兴趣的:(C++,c,.net,xml,C#)