網(wǎng)站的rss一般以兩種形式引用。一種是已經(jīng)存在的xml文件,然后在更新數(shù)據(jù)庫(kù)的時(shí)候?qū)ζ溥M(jìn)行更新,或者使用其它維護(hù)程序?yàn)槠涓?。另一種是在動(dòng)態(tài)生成rss文件,即在訪問(wèn)某一個(gè)地址的時(shí)候,服務(wù)端方法從數(shù)據(jù)庫(kù)讀取最新記錄,生成rss文件,返回給訪問(wèn)者。
現(xiàn)講述動(dòng)態(tài)生成rss文件的方法。
動(dòng)態(tài)生成rss文件也基本有兩種方法,一種是用字符串累加的方法,另一種是使用xml文檔生成的方法。字符串累加的方法也比較簡(jiǎn)單,我也就不多說(shuō)了,這里著重說(shuō)一下生成xmldocument的方法,包括各種節(jié)點(diǎn)的創(chuàng)建,屬性的創(chuàng)建等。當(dāng)然在此也有必要說(shuō)明一下為什么采用后者,因?yàn)楹笳叻蟲(chóng)ml dom標(biāo)準(zhǔn),有利于你認(rèn)識(shí)dom模型,并且構(gòu)造速度更快,構(gòu)造出的xml文檔更不容易出錯(cuò),其中有一些細(xì)節(jié)我也會(huì)做一些必要的講述。
主方法如下:
private void writerss()
{
xmldocument domdoc = new xmldocument();
xmldeclaration nodedeclar = domdoc.createxmldeclaration("1.0", system.text.encoding.utf8.bodyname, "yes");
domdoc.appendchild(nodedeclar);
//如果rss有樣式表文件的話,加上這兩句
xmlprocessinginstruction nodestylesheet = domdoc.createprocessinginstruction("xml-stylesheet","type=/"text/css/" href=/"rss.css/"");
domdoc.appendchild(nodestylesheet);
xmlelement root = domdoc.createelement("rss");
root.setattribute("version","2.0"); //添加屬性結(jié)點(diǎn)
domdoc.appendchild(root);
xmlelement chnode = domdoc.createelement("channel");
root.appendchild(chnode);
xmlelement element = domdoc.createelement("title");
xmlnode textnode = domdoc.createtextnode("搜狐焦點(diǎn)新聞"); //文本結(jié)點(diǎn)
element.appendchild(textnode);
chnode.appendchild(element);
element = domdoc.createelement("link");
textnode = domdoc.createtextnode("http://www.sohu.com");
element.appendchild(textnode);
chnode.appendchild(element);
element = domdoc.createelement("description"); //引用結(jié)點(diǎn)
xmlnode cdatanode = domdoc.createcdatasection("即時(shí)報(bào)道國(guó)內(nèi)外時(shí)政大事,解讀環(huán)球焦點(diǎn)事件");
element.appendchild(cdatanode);
chnode.appendchild(element);
datatable dt = getdatatab(); //訪問(wèn)數(shù)據(jù)庫(kù),獲取要在rss中顯示的記錄
foreach(datarow dr in dt.rows)
{
element = domdoc.createelement("item");
//...
//創(chuàng)建內(nèi)容結(jié)點(diǎn),常見(jiàn)的如title,description,link,pubdate,創(chuàng)建方法同上
//...
chnode.appendchild(element);
}
//輸出
xmltextwriter objtextwrite = new xmltextwriter(this.response.outputstream,system.text.encoding.utf8);
domdoc.writeto(objtextwrite);
objtextwrite.flush();
objtextwrite.close();
}
輸出結(jié)果如下(item部分是為說(shuō)明實(shí)例手工添加):
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<channel>
<title>搜狐焦點(diǎn)新聞</title>
<link>http://www.sohu.com</link>
<description>
<![cdata[即時(shí)報(bào)道國(guó)內(nèi)外時(shí)政大事,解讀環(huán)球焦點(diǎn)事件
]]>
</description>
<item id="">
<title></title>
<link></link>
<pubdate>2006-10-15 21:59:36</pubdate>
</item>
<item id="">
<title></title>
<link></link>
<pubdate>2006-10-15 10:33:53</pubdate>
</item>
<title>[中介][出售住宅]明發(fā)國(guó)際新城3房2廳2衛(wèi)93萬(wàn)元/套</title>
<link>http://www.ewhouse.com/houseinfo.aspx?publishid=3440</link>
<pubdate>2006-10-12 10:50:18</pubdate>
</item>
</channel>
</rss>
有幾點(diǎn)值得說(shuō)明的有:
1、 createtextnode,即創(chuàng)建文本結(jié)點(diǎn)
有人習(xí)慣使用innertext來(lái)添加結(jié)點(diǎn)中的文本,雖然結(jié)果是一樣的,但是要知道在dom中文本也是結(jié)點(diǎn),既然要符合dom標(biāo)準(zhǔn),就要進(jìn)行到底!
2、 輸出
我在實(shí)例中使用xmltextwriter輸出。
實(shí)際還可以使用如下:
response.contenttype = "application/xml"; // 輸出并按xml數(shù)據(jù)顯示
response.write(domdoc.innerxml);
但是,使用xmltextwriter輸出更快,所以也建議使用這個(gè)方法。
新聞熱點(diǎn)
疑難解答
圖片精選