在Web站点中创建和使用Rss源(一)
using System;
using System.Data;
using System.Configuration;
using System.Xml;
using System.Text;
using System.IO;
using System.Collections.Generic;
/// <summary>
/// Rss 的摘要说明
/// </summary>
public class Rss
{
private Stream outputStream;
public Stream OutputStream
{
get { return outputStream; }
set { outputStream = value; }
}
private string rssTitle;
public string RssTitle
{
get { return rssTitle; }
set { rssTitle = value; }
}
private string publisherUrl;
public string PublisherUrl
{
get { return publisherUrl; }
set { publisherUrl = value; }
}
private string description;
public string Description
{
get { return description; }
set { description = value; }
}
private string copyright;
public string Copyright
{
get { return copyright; }
set { copyright = value; }
}
private string generator;
public string Generator
{
get { return generator; }
set { generator = value; }
}
private DataSet itemSource;
public DataSet ItemSource
{
get { return itemSource; }
set { itemSource = value; }
}
private string itemTitleField;
public string ItemTitleField
{
get { return itemTitleField; }
set { itemTitleField = value; }
}
private string itemUrlField;
public string ItemUrlField
{
get { return itemUrlField; }
set { itemUrlField = value; }
}
private string itemDescriptionField;
public string ItemDescriptionField
{
get { return itemDescriptionField; }
set { itemDescriptionField = value; }
}
private string itemPublicationDateField;
public string ItemPublicationDateField
{
get { return itemPublicationDateField; }
set { itemPublicationDateField = value; }
}
private string itemAuthor;
public string ItemAuthor
{
get { return itemAuthor; }
set { itemAuthor = value; }
}
public static void PublishRss(Rss r)
{
XmlTextWriter writer = new XmlTextWriter(r.OutputStream, Encoding.UTF8);
writer.WriteStartDocument();
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", GetRssDate(row[r.ItemPublicationDateField]));
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
}
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 St
补充:Web开发 , 其他 ,