生成RSS的類(RSS Feed Helper Class)

該RSS類的代碼:

using System;
using System.Xml;
/// <summary>
/// Enables the generation of an RSS feed
/// </summary>
public class RSSFeedGenerator
{
    XmlTextWriter writer;
    public RSSFeedGenerator( System.IO.Stream stream, System.Text.Encoding encoding )
    {
        writer = new XmlTextWriter(stream, encoding);
        writer.Formatting = Formatting.Indented;
    }
    public RSSFeedGenerator( System.IO.TextWriter w )
    {
        writer = new XmlTextWriter(w);
        writer.Formatting = Formatting.Indented;
    }
    /// <summary>
    /// Writes the beginning of the RSS document
    /// </summary>
    public void WriteStartDocument()
    {
        writer.WriteStartDocument();
        writer.WriteStartElement("rss");
        writer.WriteAttributeString("version","2.0");
    }
    /// <summary>
    /// Writes the end of the RSS document
    /// </summary>
    public void WriteEndDocument()
    {
        writer.WriteEndElement(); //rss
        writer.WriteEndDocument();
    }
    /// <summary>
    /// Closes this stream and the underlying stream
    /// </summary>
    public void Close()
    {
        writer.Flush();
        writer.Close();
    }
    /// <summary>
    /// Begins a new channel in the RSS document
    /// </summary>
    /// <param name="title"></param>
    /// <param name="link"></param>
    /// <param name="description"></param>
    public void WriteStartChannel(string title, string link, string description, string copyright, string webMaster)
    {
        writer.WriteStartElement("channel");
        writer.WriteElementString("title",title);
        writer.WriteElementString("link",link);
        writer.WriteElementString("description",description);
        writer.WriteElementString("language","en-gb");
        writer.WriteElementString("copyright",copyright);
        writer.WriteElementString("generator","Developer Fusion RSS Feed Generator v1.0");
        writer.WriteElementString("webMaster",webMaster);
        writer.WriteElementString("lastBuildDate",DateTime.Now.ToString("r"));
        writer.WriteElementString("ttl","20");
       
    }
    /// <summary>
    /// Writes the end of a channel in the RSS document
    /// </summary>
    public void WriteEndChannel()
    {
        writer.WriteEndElement(); //channel
    }
    /// <summary>
    /// Writes an item to a channel in the RSS document
    /// </summary>
    /// <param name="title"></param>
    /// <param name="link"></param>
    /// <param name="description"></param>
    /// <param name="author"></param>
    /// <param name="publishedDate"></param>
    /// <param name="category"></param>
    public void WriteItem(string title, string link, string description, string author, DateTime publishedDate, string subject)
    {
        writer.WriteStartElement("item");
        writer.WriteElementString("title",title);
        writer.WriteElementString("link",link);
        writer.WriteElementString("description",description);
        writer.WriteElementString("author",author);
        writer.WriteElementString("pubDate",publishedDate.ToString("r"));
        //writer.WriteElementString("category",category);
        writer.WriteElementString("subject",subject);
        writer.WriteEndElement();

使用示例:
// set the content type
Page.Response.ContentType = "text/xml";
// create a RSS feed generator for the output
RSSFeedGenerator gen = new RSSFeedGenerator(Page.Response.Output);
gen.WriteStartDocument();
gen.WriteStartChannel("Developer Fusion RSS Feed",
    "http://www.developerfusion.com/",
    "Summary of the latest articles published on Developer Fusion",
    "Copyright © Developer Fusion Ltd, 1999-2004", "James Crowley" );
// generate the items here
gen.WriteItem("Using ADO.NET with SQL Server",
    "http://www.developerfusion.com/show/4278/",
    "An extensive examination of using ADO.NET to access SQL Server databases, from establishing connections and executing stored procedures, to connection pools, data readers and data sets.",
    "James Crowley", DateTime.Now, ".NET");
// clear up
gen.WriteEndChannel();
gen.WriteEndDocument();
gen.Close();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章