实战篇-六十六行完成简洁的Rss输出类

出处:http://www.cnblogs.com/cyq1162/archive/2010/12/15/1906869.html

具体RSS是什么也就不多介绍了,相关的语法和资料,可详见:w3school

Rss 文档示例:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> <? xmlversion="1.0"encoding="utf-8" ?>
< rss version ="2.0" >
< channel >
< title > AutumnGurdenPage </ title >
< link > http://cyqdata.com </ link >
< description > AutumnParkisQBlogtheofficialsite,createdbythepassingautumn,basedontheframeworkdevelopedcyqdatadatalayersupportsmultipleusers,multiplelanguages,multipledatabases(access,mssql,oracle),directorylevelurlandotherpowerfulblogsystem </ description >
< item >
< title > RSSTutorial </ title >
< link > http://www.cyqdata.com/rss </ link >
< description > NewRSStutorialoncyqdata.com </ description >
</ item >
< item >
< title > XMLTutorial </ title >
< link > http://www.cyqdata.com/cyq1162/rss </ link >
< description > NewXMLtutorialoncyqdata.com </ description >
</ item >
</ channel >
</ rss >

从示例文档上看,Rss的基本构成:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1:channel一个

2:channel属性若干,有必选和N多可选。

3:channel下有N个Item项,Item也有若干选项。

为此,简单的说,只要输出以上格式的xml,简单的Rss也即成功了,下面进行代码实战。

一:实体类创建

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 要写成完整点的Rss版,费劲的不是要想怎么输出,而是要知道RSS都有些什么属性,虽然基本文档示例上看相当简洁明了,

不过实现其属性却不是那么一点半点的,为此,prop一个一个敲属性的时候,就费了我不少时间

下面上我辛苦敲写的实体类:

1:RssChannel

RssChannel
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> public class RssChannel
{
#region 必选
private string _Title;
/// <summary>
/// 定义频道的标题
/// </summary>
public string Title
{
get
{
return _Title;
}
set
{
_Title
= value;
}
}
private string _Link;
/// <summary>
/// 定义指向频道的超链接
/// </summary>
public string Link
{
get
{
return _Link;
}
set
{
_Link
= value;
}
}
private string _Description;
/// <summary>
/// 描述频道
/// </summary>
public string Description
{
get
{
return _Description;
}
set
{
_Description
= value;
}
}
#endregion

#region 可选
private string _Category;
/// <summary>
/// 为feed定义所属的一个或多个种类
/// </summary>
public string Category
{
get
{
return _Category;
}
set
{
_Category
= value;
}
}
private string _Cloud;
/// <summary>
/// 注册进程,以获得feed更新的立即通知
/// </summary>
public string Cloud
{
get
{
return _Cloud;
}
set
{
_Cloud
= value;
}
}
private string _Copyright;
/// <summary>
/// 告知版权资料
/// </summary>
public string Copyright
{
get
{
return _Copyright;
}
set
{
_Copyright
= value;
}
}
private string _Docs;
/// <summary>
/// 规定指向当前RSS文件所用格式说明的URL
/// </summary>
public string Docs
{
get
{
return _Docs;
}
set
{
_Docs
= value;
}
}
private string _Generator;
/// <summary>
/// 规定指向当前RSS文件所用格式说明的URL
/// </summary>
public string Generator
{
get
{
return _Generator;
}
set
{
_Generator
= value;
}
}
private string _Language = " zh-cn " ;
/// <summary>
/// 规定编写feed所用的语言
/// </summary>
public string Language
{
get
{
return _Language;
}
set
{
_Language
= value;
}
}

private string _LastBuildDate;
/// <summary>
/// 定义feed内容的最后修改日期
/// </summary>
public string LastBuildDate
{
get
{
return _LastBuildDate;
}
set
{
_LastBuildDate
= value;
}
}
private string _ManagingEditor;
/// <summary>
/// 定义feed内容编辑的电子邮件地址
/// </summary>
public string ManagingEditor
{
get
{
return _ManagingEditor;
}
set
{
_ManagingEditor
= value;
}
}
private string _PubDate;
/// <summary>
/// 为feed的内容定义最后发布日期
/// </summary>
public string PubDate
{
get
{
return _PubDate;
}
set
{
_PubDate
= value;
}
}
private string _Rating;
/// <summary>
/// feed的PICS级别
/// </summary>
public string Rating
{
get
{
return _Rating;
}
set
{
_Rating
= value;
}
}
private string _SkipDays;
/// <summary>
/// 规定忽略feed更新的天
/// </summary>
public string SkipDays
{
get
{
return _SkipDays;
}
set
{
_SkipDays
= value;
}
}
private string _SkipHours;
/// <summary>
/// 规定忽略feed更新的小时
/// </summary>
public string SkipHours
{
get
{
return _SkipHours;
}
set
{
_SkipHours
= value;
}
}
private string _TextInput;
/// <summary>
/// 规定应当与feed一同显示的文本输入域
/// </summary>
public string TextInput
{
get
{
return _TextInput;
}
set
{
_TextInput
= value;
}
}
private string _Ttl;
/// <summary>
/// 指定从feed源更新此feed之前,feed可被缓存的分钟数
/// </summary>
public string Ttl
{
get
{
return _Ttl;
}
set
{
_Ttl
= value;
}
}
private string _WebMaster;
/// <summary>
/// 定义此feed的web管理员的电子邮件地址
/// </summary>
public string WebMaster
{
get
{
return _WebMaster;
}
set
{
_WebMaster
= value;
}
}
#endregion
private string _RssImage;
/// <summary>
/// 定义此 feed 的 图片Logo
/// </summary>
public string RssImage
{
get
{
return _RssImage;
}
set
{
_RssImage = value;
}
}
private List < RssItem > _Items = new List < RssItem > ();
public List < RssItem > Items
{
get
{
return _Items;
}
set
{
_Items
= value;
}
}
}

2:RssItem

RssItem
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> public class RssItem
{
#region 必选
private string _Title;
/// <summary>
/// 定义频道的标题
/// </summary>
public string Title
{
get
{
return _Title;
}
set
{
_Title
= value;
}
}
private string _Link;
/// <summary>
/// 定义指向频道的超链接
/// </summary>
public string Link
{
get
{
return _Link;
}
set
{
_Link
= value;
}
}
private string _Description;
/// <summary>
/// 描述频道
/// </summary>
public string Description
{
get
{
return _Description;
}
set
{
_Description
= value;
}
}
#endregion

#region 可选
private string _Category;
/// <summary>
/// 为feed定义所属的一个或多个种类
/// </summary>
public string Category
{
get
{
return _Category;
}
set
{
_Category
= value;
}
}
private string _Author;
/// <summary>
/// 规定项目作者的电子邮件地址
/// </summary>
public string Author
{
get
{
return _Author;
}
set
{
_Author
= value;
}
}
private string _Comments;
/// <summary>
/// 允许项目连接到有关此项目的注释(文件)
/// </summary>
public string Comments
{
get
{
return _Comments;
}
set
{
_Comments
= value;
}
}
private string _Enclosure;
/// <summary>
/// 允许将一个媒体文件导入一个项中
/// </summary>
public string Enclosure
{
get
{
return _Enclosure;
}
set
{
_Enclosure
= value;
}
}
private string _Guid;
/// <summary>
/// 为项目定义一个唯一的标识符
/// </summary>
public string Guid
{
get
{
return _Guid;
}
set
{
_Guid
= value;
}
}
private string _PubDate;
/// <summary>
/// 定义此项目的最后发布日期
/// </summary>
public string PubDate
{
get
{
return _PubDate;
}
set
{
_PubDate
= value;
}
}
private string _Source;
/// <summary>
/// 为此项目指定一个第三方来源
/// </summary>
public string Source
{
get
{
return _Source;
}
set
{
_Source
= value;
}
}
#endregion
}

3:RssImage--图片Logo输出

RssImage
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> public class RssImage
{
#region 必选
private string _Url;
/// <summary>
/// 图片地址
/// </summary>
public string Url
{
get
{
return _Url;
}
set
{
_Url
= value;
}
}
private string _Title;
/// <summary>
/// 图片标题
/// </summary>
public string Title
{
get
{
return _Title;
}
set
{
_Title
= value;
}
}
private string _Link;
/// <summary>
/// 提供图片的站点链接
/// </summary>
public string Link
{
get
{
return _Link;
}
set
{
_Link
= value;
}
}
private string _Description;
/// <summary>
/// 描述频道
/// </summary>
public string Description
{
get
{
return _Description;
}
set
{
_Description
= value;
}
}
#endregion
}

最有技术含量的,就是上面三个实体类的敲入了,看着w3规范的属性,一个一个的敲,很累人。

OK,有了三个的实体类,接下的事情变的相当简单了。

二:创建简洁RSS类

1:新建Rss类--构造函数相当简单的初始化了一下基本数据。

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> public class Rss
{
XmlDocumentrssDoc;
public RssChannel channel;
public Rss()
{
rssDoc
= new XmlDocument( false );
rssDoc.LoadXml(
" <?xmlversion=/ " 1.0 / " encoding=/ " utf - 8 / " ?><rssversion=/ " 2.0 / " ><channel></channel></rss> " );
channel
= new RssChannel();
}
}

2:创建生成RSS--反射实体创建xml元素。

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> private void BuildRss()
{
XmlNodecNode
= rssDoc.DocumentElement.ChildNodes[ 0 ]; // 取得channel元素
ForeachCreateChild(cNode,channel); // Channel处理
if (channel.RssImage != null )
{
ForeachCreateChild(Create(
" image " , null ,cNode),channel.RssImage); // Channel-Image处理
}
if (channel.Items.Count > 0 )
{
foreach (RssItemitem in channel.Items)
{
ForeachCreateChild(Create(
" item " , null ,cNode),item); // Channel-Items处理
}
}
}

函数分解1:创建节点元素

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> private XmlNodeCreate( string name, string value,XmlNodeparent)
{
XmlElementxNode
= rssDoc.CreateElement(name);
if ( string .IsNullOrEmpty(value))
{
xNode.InnerXml
= value;
}
parent.AppendChild(xNode
as XmlNode);
return xNode as XmlNode;
}

函数分解2:反射循环创建子节点

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> private void ForeachCreateChild(XmlNodeparent, object obj)
{
object propValue = null ;
PropertyInfo[]pis
= obj.GetType().GetProperties();
for ( int i = 0 ;i < pis.Length;i ++ )
{
if (pis[i].Name == " Items " || pis[i].Name == " Image " )
{
continue ;
}
propValue
= pis[i].GetValue(obj, null );
if (propValue == null || propValue == DBNull.Value)
{
continue ;
}
if (pis[i].Name == " Description " )
{
propValue
= " <![CDATA[ " + propValue.ToString() + " ]]> " ;
}
Create(pis[i].Name.Substring(
0 , 1 ).ToLower() + pis[i].Name.Substring( 1 ),propValue.ToString(),parent);
}
}

3:输出RSS

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> public string OutXml
{
get
{
BuildRss();
return rssDoc.OuterXml;
}
}

至此,一个相当简洁的RSS类就完成了,小小数了一下,RSS类一共花了66代码[要是数上实体类,那代码行就相当的惊人了],目前在保持良好阅读格式下也只能把代码简化到这种程度了。

三:测试使用示例代码

1:怎么使用,这个相当简单了

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> public class RssDemo
{
public string GetRss()
{
Rssrss
= new Rss();
rss.channel.Title
= " 秋色园 " ;
rss.channel.Link
= " http://www.cyqdata.com " ;
rss.channel.Description
= " 秋色园-QBlog-PowerbyBlog.CYQ " ;
for ( int i = 0 ;i < 10 ;i ++ )
{
RssItemitem
= new RssItem();
item.Title
= string .Format( " 第{0}项 " ,i);
item.Link
= " http://www.cyqdata.com " ;
item.Description
= " 很长很长的内容 " ;
rss.channel.Items.Add(item);
}
return rss.OutXml;
}
}

至此,本文就结束了,代码太短,大伙可能刷的一眼就看完了,不过打完收工也到时间了。

你可能感兴趣的:(rss)