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

    xiaoxiao2022-07-09  60

    前言:

    前天为 秋色园 增加了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

    2:RssItem

    RssItem

    3:RssImage--图片Logo输出

    RssImage

     

    最有技术含量的,就是上面三个实体类的敲入了,看着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( 0 1 ).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!!

    版权声明:本文原创发表于博客园,作者为路过秋天,原文链接:

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

    相关资源:敏捷开发V1.0.pptx
    最新回复(0)