網(wǎng)站的RSS一般以?xún)煞N形式引用。一種是已經(jīng)存在的xml文件,然后在更新數據庫的時(shí)候對其進(jìn)行更新,或者使用其它維護程序為其更新。另一種是在動(dòng)態(tài)生成RSS文件,即在訪(fǎng)問(wèn)某一個(gè)地址的時(shí)候,服務(wù)端方法從數據庫讀取最新記錄,生成RSS文件,返回給訪(fǎng)問(wèn)者。
現講述動(dòng)態(tài)生成RSS文件的方法。
動(dòng)態(tài)生成RSS文件也基本有兩種方法,一種是用字符串累加的方法,另一種是使用xml文檔生成的方法。字符串累加的方法也比較簡(jiǎn)單,我也就不多說(shuō)了,這里著(zhù)重說(shuō)一下生成XmlDocument的方法,包括各種節點(diǎn)的創(chuàng )建,屬性的創(chuàng )建等。當然在此也有必要說(shuō)明一下為什么采用后者,因為后者符合XML DOM標準,有利于你認識dom模型,并且構造速度更快,構造出的xml文檔更不容易出錯,其中有一些細節我也會(huì )做一些必要的講述。
主方法如下:
private void WriteRSS()
{
XmlDocument domDoc = new XmlDocument();
XmlDeclaration nodeDeclar = domDoc.CreateXmlDeclaration("1.0", System.Text.Encoding.UTF8.BodyName, "yes");
domDoc.AppendChild(nodeDeclar);
//如果rss有樣式表文件的話(huà),加上這兩句
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"); //添加屬性結點(diǎn)
domDoc.AppendChild(root);
XmlElement chnode = domDoc.CreateElement("channel");
root.AppendChild(chnode);
XmlElement element = domDoc.CreateElement("title");
XmlNode textNode = domDoc.CreateTextNode("搜狐焦點(diǎn)新聞"); //文本結點(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"); //引用結點(diǎn)
XmlNode cDataNode = domDoc.CreateCDataSection("即時(shí)報道國內外時(shí)政大事,解讀環(huán)球焦點(diǎn)事件");
element.AppendChild(cDataNode);
chnode.AppendChild(element);
DataTable dt = GetDataTab(); //訪(fǎng)問(wèn)數據庫,獲取要在rss中顯示的記錄
foreach(DataRow dr in dt.Rows)
{
element = domDoc.CreateElement("item");
//...
//創(chuàng )建內容結點(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();
}
輸出結果如下(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í)報道國內外時(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ā)國際新城3房2廳2衛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 )建文本結點(diǎn)
有人習慣使用InnerText來(lái)添加結點(diǎn)中的文本,雖然結果是一樣的,但是要知道在DOM中文本也是結點(diǎn),既然要符合DOM標準,就要進(jìn)行到底!
2、 輸出
我在實(shí)例中使用XmlTextWriter輸出。
實(shí)際還可以使用如下:
Response.ContentType = "application/xml"; // 輸出并按xml數據顯示
Response.Write(domDoc.InnerXml);
但是,使用XmlTextWriter輸出更快,所以也建議使用這個(gè)方法。
聯(lián)系客服