2015-05-11  643 views 评论

关于在.NET中实现RSS 后台生成XML代码

在后台中使用以下测试代码可生成XML代码:(注:需要引入using System.Xml.Linq;)

<p align="left"><span lang="EN-US">        XElementtestXml = new XElement("Message", newXElement[]{//Message</span>为顶级节点</p>
<p align="left"><span lang="EN-US">            newXElement("user","古成</span><span lang="EN-US">"</span><span lang="EN-US">),</span></p>
<p align="left"><span lang="EN-US">            newXElement("mood","寻找实现RSS过程中……</span><span lang="EN-US">"</span><span lang="EN-US">),</span></p>
<p align="left"><span lang="EN-US">            newXElement("settime",DateTime.Now.ToString("HH:mm")),</span></p>
<p align="left"><span lang="EN-US">            newXElement("sendto","me")//user</span>,<span lang="EN-US">mood</span>,<span lang="EN-US">settime</span>,<span lang="EN-US">sendto</span><span style="font-family: NSimsun;">为同级二级节点</span></p>
<p align="left"><span lang="EN-US">          });</span></p>
<p align="left"><span lang="EN-US">        Response.Write(testXml.ToString());</span></p>
<p align="left"><span lang="EN-US">        Response.End();</span></p>

测试成功!
由此结合rss2.0的规范可以照着上面的测试代码写出RSS生成代码:

<div>在pageload中添加:</div>
<div>
<p align="left"><span lang="EN-US">        XElementrss = new XElement("rss");</span></p>
<p align="left"><span lang="EN-US">        rss.SetAttributeValue("version", "2.0");//</span>设置<span lang="EN-US">rss</span>部的版本属性</p>
<p align="left"><span lang="EN-US">                XElementchannel = new XElement("channel", newXElement[]{//channel</span>为顶级节点</p>
<p align="left"><span lang="EN-US">          new XElement("title","</span>大学生网上创业联盟<span lang="EN-US">RSS"</span><span lang="EN-US">),</span></p>
<p align="left"><span lang="EN-US">          new XElement("link","<a href="http://www.dxscywm.com/" target="_blank">http://www.dxscywm.com</a>"),</span></p>
<p align="left"><span lang="EN-US">          new XElement("description","</span>大学生网上创业联盟最新资讯<span lang="EN-US">?"</span><span lang="EN-US">),</span></p>
<p align="left"><span lang="EN-US">        });</span></p>
<p align="left"><span lang="EN-US">        for (int i = 0; i &lt; 5; i++)</span></p>
<p align="left"><span lang="EN-US">        {</span></p>
<p align="left"><span lang="EN-US">            XElementitem = new XElement("item", newXElement[]{</span></p>
<p align="left"><span lang="EN-US">           new XElement("title","</span>标题<span lang="EN-US">"</span><span lang="EN-US">+i),</span></p>
<p align="left"><span lang="EN-US">           new XElement("link","url"),</span></p>
<p align="left"><span lang="EN-US">           new XElement("description","</span>描述<span lang="EN-US">"</span><span lang="EN-US">+i),</span></p>
<p align="left"><span lang="EN-US">           new XElement("pubDate",DateTime.UtcNow.ToString()),</span></p>
<p align="left"><span lang="EN-US">         });</span></p>
<p align="left"><span lang="EN-US">            channel.Add(item);//</span>为顶级节点<span lang="EN-US">channel</span>添加子节点<span lang="EN-US">item</span></p>
<p align="left"><span lang="EN-US">        }</span></p>
<p align="left"><span lang="EN-US">        rss.Add(channel);</span></p>
<p align="left"><span lang="EN-US">        Response.Write(rss.ToString());</span></p>
<p align="left"><span lang="EN-US">        Response.End();</span></p>

得到生成的源文件代码为:

<p align="left"><span lang="EN-US">&lt;</span><span lang="EN-US">rss</span><span lang="EN-US"> version="2.0"&gt;</span></p>
<p align="left"><span lang="EN-US">  &lt;channel&gt;</span></p>
<p align="left"><span lang="EN-US">    &lt;title&gt;</span>大<span lang="EN-US">¨</span>学<span lang="EN-US">ì</span>生<span lang="EN-US">¨</span>网<span lang="EN-US">?</span>上<span lang="EN-US">?</span>创业<span lang="EN-US">°</span>联<span lang="EN-US">a</span>盟<span lang="EN-US">?RSS&lt;/title&gt;</span></p>
<p align="left"><span lang="EN-US">    &lt;link&gt;<a href="http://www.dxscywm.com/" target="_blank">http://www.dxscywm.com</a>&lt;/link&gt;</span></p>
<p align="left"><span lang="EN-US">    &lt;description&gt;</span>大<span lang="EN-US">¨</span>学<span lang="EN-US">ì</span>生<span lang="EN-US">¨</span>网<span lang="EN-US">?</span>上<span lang="EN-US">?</span>创业<span lang="EN-US">°</span>联<span lang="EN-US">a</span>盟<span lang="EN-US">?</span>最<span lang="EN-US">?</span>新<span lang="EN-US">?</span>资<span lang="EN-US">¨</span>讯<span lang="EN-US">?&lt;/description&gt;</span></p>
<p align="left"><span lang="EN-US">    &lt;item&gt;</span></p>
<p align="left"><span lang="EN-US">      &lt;title&gt;</span>标<span lang="EN-US">¨</span>题<span lang="EN-US">a0&lt;/title&gt;</span></p>
<p align="left"><span lang="EN-US">      &lt;link&gt;<a href="http://www.baidu.com/" target="_blank">http://www.baidu.com</a>&lt;/link&gt;</span></p>
<p align="left"><span lang="EN-US">      &lt;description&gt;</span>描<span lang="EN-US">¨¨</span>述<span lang="EN-US">?0&lt;/description&gt;</span></p>
<p align="left"><span lang="EN-US">      &lt;pubData&gt;2012/7/122:08:08&lt;/pubData&gt;</span></p>
<p align="left"><span lang="EN-US">    &lt;/item&gt;</span></p>
<p align="left"><span lang="EN-US">    &lt;item&gt;</span></p>
<p align="left"><span lang="EN-US">      &lt;title&gt;</span>标<span lang="EN-US">¨</span>题<span lang="EN-US">a1&lt;/title&gt;</span></p>
<p align="left"><span lang="EN-US">      &lt;link&gt;<a href="http://www.baidu.com/" target="_blank">http://www.baidu.com</a>&lt;/link&gt;</span></p>
<p align="left"><span lang="EN-US">      &lt;description&gt;</span>描<span lang="EN-US">¨¨</span>述<span lang="EN-US">?1&lt;/description&gt;</span></p>
<p align="left"><span lang="EN-US">      &lt;pubData&gt;2012/7/122:08:08&lt;/pubData&gt;</span></p>
<p align="left"><span lang="EN-US">    &lt;/item&gt;</span></p>
<p align="left"><span lang="EN-US">    &lt;item&gt;</span></p>
<p align="left"><span lang="EN-US">      &lt;title&gt;</span>标<span lang="EN-US">¨</span>题<span lang="EN-US">a2&lt;/title&gt;</span></p>
<p align="left"><span lang="EN-US">      &lt;link&gt;<a href="http://www.baidu.com/" target="_blank">http://www.baidu.com</a>&lt;/link&gt;</span></p>
<p align="left"><span lang="EN-US">      &lt;description&gt;</span>描<span lang="EN-US">¨¨</span>述<span lang="EN-US">?2&lt;/description&gt;</span></p>
<p align="left"><span lang="EN-US">      &lt;pubData&gt;2012/7/122:08:08&lt;/pubData&gt;</span></p>
<p align="left"><span lang="EN-US">    &lt;/item&gt;</span></p>
<p align="left"><span lang="EN-US">    &lt;item&gt;</span></p>
<p align="left"><span lang="EN-US">      &lt;title&gt;</span>标<span lang="EN-US">¨</span>题<span lang="EN-US">a3&lt;/title&gt;</span></p>
<p align="left"><span lang="EN-US">      &lt;link&gt;<a href="http://www.baidu.com/" target="_blank">http://www.baidu.com</a>&lt;/link&gt;</span></p>
<p align="left"><span lang="EN-US">      &lt;description&gt;</span>描<span lang="EN-US">¨¨</span>述<span lang="EN-US">?3&lt;/description&gt;</span></p>
<p align="left"><span lang="EN-US">      &lt;pubData&gt;2012/7/122:08:08&lt;/pubData&gt;</span></p>
<p align="left"><span lang="EN-US">    &lt;/item&gt;</span></p>
<p align="left"><span lang="EN-US">    &lt;item&gt;</span></p>
<p align="left"><span lang="EN-US">      &lt;title&gt;</span>标<span lang="EN-US">¨</span>题<span lang="EN-US">a4&lt;/title&gt;</span></p>
<p align="left"><span lang="EN-US">      &lt;link&gt;<a href="http://www.baidu.com/" target="_blank">http://www.baidu.com</a>&lt;/link&gt;</span></p>
<p align="left"><span lang="EN-US">      &lt;description&gt;</span>描<span lang="EN-US">¨¨</span>述<span lang="EN-US">?4&lt;/description&gt;</span></p>
<p align="left"><span lang="EN-US">      &lt;pubData&gt;2012/7/122:08:08&lt;/pubData&gt;</span></p>
<p align="left"><span lang="EN-US">    &lt;/item&gt;</span></p>
<p align="left"><span lang="EN-US">  &lt;/channel&gt;</span></p>
<p align="left"><span lang="EN-US">&lt;/</span><span lang="EN-US">rss</span><span lang="EN-US">&gt;</span></p>

给我留言

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: