前言:

前天为 秋色园 增加了Rss  输出,因此相应的底层框架 CYQ.Data 也增加了Rss类,这里单独抽出来重新写了个通用Rss类,独成一篇。

Rss测试输出地址可见:

主站:http://www.cyqdata.com/rss  

用户:http://www.cyqdata.com/cnblogs/rss

 

 

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

Rss 文档示例:

<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<channel>
  
<title>Autumn Gurden Page</title>
  
<link>http://cyqdata.com</link>
  
<description>Autumn Park is QBlog the official site, created by the passing autumn, based on the framework developed cyqdata data layer supports multiple users, multiple languages, multiple databases(access,mssql,oracle), directory level url and other powerful blog system</description>
  
<item>
    
<title>RSS Tutorial</title>
    
<link>http://www.cyqdata.com/rss</link>
    
<description>New RSS tutorial on cyqdata.com</description>
  
</item>
  
<item>
    
<title>XML Tutorial</title>
    
<link>http://www.cyqdata.com/cyq1162/rss</link>
    
<description>New XML tutorial on cyqdata.com</description>
  
</item>
</channel>
</rss> 

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

1:channel一个

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

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

 

 

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

 

一:实体类创建

要写成完整点的Rss版,费劲的不是要想怎么输出,而是要知道RSS都有些什么属性,虽然基本文档示例上看相当简洁明了,

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

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

1:RssChannel

RssChannel
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
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
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类--构造函数相当简单的初始化了一下基本数据。

    public class Rss
    {
        XmlDocument rssDoc;
        public RssChannel channel;
        
public Rss()
        {
            rssDoc 
= new XmlDocument();
            rssDoc.LoadXml(
"<?xml version=\"1.0\" encoding=\"utf-8\"?><rss version=\"2.0\"><channel></channel></rss>");
            channel 
= new RssChannel();
        }
    }

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

        private void BuildRss()
        {
            XmlNode cNode 
= 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 (RssItem item in channel.Items)
                {
                    ForeachCreateChild(Create(
"item"null,cNode), item);//Channel-Items处理
                }
            }
        }

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

        private XmlNode Create(string name,string value,XmlNode parent)
        {
            XmlElement xNode
=rssDoc.CreateElement(name);
            
if (!string.IsNullOrEmpty(value))
            {
                xNode.InnerXml 
= value;
            }
            parent.AppendChild(xNode 
as XmlNode);
            
return xNode as XmlNode;
        }

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

        private void ForeachCreateChild(XmlNode parent, 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(
01).ToLower() + pis[i].Name.Substring(1), propValue.ToString(), parent);
            }
        }

3:输出RSS

        public string OutXml
        {
            
get
            {
                BuildRss();
                
return rssDoc.OuterXml;
            }
        }

 

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

 

 三:测试使用示例代码

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

    public class RssDemo
    {
        
public string GetRss()
        {
            Rss rss 
= new Rss();
            rss.channel.Title 
= "秋色园";
            rss.channel.Link 
= "http://www.cyqdata.com";
            rss.channel.Description 
= "秋色园-QBlog-Power by Blog.CYQ";
            
for (int i = 0; i < 10; i++)
            {
                RssItem item 
= new RssItem();
                item.Title 
= string.Format("第{0}项", i);
                item.Link 
= "http://www.cyqdata.com";
                item.Description 
= "很长很长的内容";
                rss.channel.Items.Add(item);
            }
            
return rss.OutXml;
        }
    }

 

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

 

最后:好久没写文章了,今天抽点时间写成一篇,希望本文对大伙有点帮助,谢谢支持~

PS:传说点一下推荐会有10个园豆,喜欢麻烦点一下“推荐”,thank you very much!!

 

 

posted on 2010-12-15 17:24  路过秋天  阅读(2842)  评论(29编辑  收藏  举报
路过秋天