星期二, 8月 26, 2008

(linq to xml)產生rss 2.0

最近在做這功能..順便把class貼出來..下次有需要用到的人就有範例可以參考了..Tongue out..

using System;
using System.Collections.Generic;
using System.Data;
using System.Xml.Linq;

/// <summary>
/// 產生rss 2.0的class
/// </summary>
public class RssGenerator
{
#region 欄位,預設值
private XNamespace _nameSpace = "http://yourDomain";
private string _language = "zh-tw";
private string _pubDate = DateTime.UtcNow.ToString();
private string _lastBuildDate = DateTime.UtcNow.ToString();
#endregion

#region 屬性,來設定基本的rss描述
List<object> liObj = new List<object>();

public string NameSpace
{
set { _nameSpace = value; }
}
public string Title { private get; set; }
public string Link { private get; set; }
public string Description { private get; set; }
public string Language
{
set { _language = value; }
}
public string PubDate
{
set { _pubDate = value; }
}
public string LastBuildDate
{
set { _lastBuildDate = value; }
}
public string Generator { private get; set; }
public string ManagingEditor { private get; set; }
public string WebMaster { private get; set; }
#endregion

/// <summary>
/// 新增rss
/// </summary>
/// <param name="dt">丟入內容datatable,ColumName要用MyRssTagName</param>
/// <returns>丟回rss的xml</returns>
public string Create(DataTable dt)
{
//加入Rss基本描述
RssBaseDescription();
//顯示要出現的內容
ItemsContent(dt);

//產生xml
XDocument xDoc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XProcessingInstruction("rss", "version=\"2.0\""),
//new XComment("這是測試用產生Rss"),
new XElement(_nameSpace + "rss",
new XAttribute("version", "2.0"),
new XElement(_nameSpace + "channel",
liObj.ToArray()
)
)
);

return Convert.ToString(xDoc);
}
/// <summary>
/// 把基本欄位加入
/// </summary>
private void RssBaseDescription()
{
liObj.Add(new XElement(_nameSpace + "title", Title));
liObj.Add(new XElement(_nameSpace + "link", Link));
liObj.Add(new XElement(_nameSpace + "description", Description));
liObj.Add(new XElement(_nameSpace + "language", _language));
liObj.Add(new XElement(_nameSpace + "pubDate", _pubDate));
liObj.Add(new XElement(_nameSpace + "lastBuildDate", _lastBuildDate));
liObj.Add(new XElement(_nameSpace + "generator", Generator));
liObj.Add(new XElement(_nameSpace + "managingEditor", ManagingEditor));
liObj.Add(new XElement(_nameSpace + "webMaster", WebMaster));
}
/// <summary>
/// 要show出rss的內容
/// </summary>
/// <param name="dt"></param>
private void ItemsContent(DataTable dt)
{
foreach (DataRow dr in dt.Rows)
{
liObj.Add(
new XElement(_nameSpace + "items",
new XElement(_nameSpace + "title", Convert.ToString(dr["title"])),
new XElement(_nameSpace + "link", Convert.ToString(dr["link"])),
new XElement(_nameSpace + "description", Convert.ToString(dr["description"])),
new XElement(_nameSpace + "pubDate", Convert.ToString(dr["pubDate"])),
new XElement(_nameSpace + "guid", Convert.ToString(dr["guid"]))
)
);
}
}
/// <summary>
/// rss items的tag
/// </summary>
public enum MyRssTagName
{
title, link, description, pubDate, guid
}
}

使用方法

RssGenerator xml = new RssGenerator();

//rss預設值
xml.NameSpace = "http://bibby.be";
xml.Title = "Rss Test";
xml.Link = "http://bibby.be";
xml.Language = "zh-tw";
xml.Generator = "aa@test.test";
xml.ManagingEditor = "aa@yahoo.com.tw";
xml.PubDate = DateTime.Now.ToString();

//要產生出來的資料
DataTable dt = new DataTable();
dt.Columns.Add(RssGenerator.MyRssTagName.title.ToString());
dt.Columns.Add(RssGenerator.MyRssTagName.link.ToString());
dt.Columns.Add(RssGenerator.MyRssTagName.description.ToString());
dt.Columns.Add(RssGenerator.MyRssTagName.guid.ToString());
dt.Columns.Add(RssGenerator.MyRssTagName.pubDate.ToString());
for (int i = 0; i < 2; i++)
{
DataRow dr = dt.NewRow();
dr[0] = "Title";
dr[1] = "http://yahoo.com.tw";
dr[2] = "just test it";
dr[3] = "idid";
dr[4] = DateTime.Now.ToString();
dt.Rows.Add(dr);
}

//string Rss = xml.Create(dt);
xml.Create(dt)

需要者自行服用..拉肚子..別找我..Vampire BatVampire BatVampire BatVampire BatVampire BatVampire BatVampire Bat..

2 則留言:

匿名 提到...

Dear Author bibby.be !
Unsuccessful idea

匿名 提到...

I want to quote your post in my blog. It can?
And you et an account on Twitter?