生活随笔
收集整理的這篇文章主要介紹了
C#与RSS亲密接触
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
講述動態(tài)生成RSS文件的方法。
動態(tài)生成RSS文件也基本有兩種方法,一種是用字符串累加的方法,另一種是使用xml文檔生成的方法。字符串累加的方法也比較簡單,我也就不多說了,這里著重說一下生成XmlDocument的方法,包括各種節(jié)點的創(chuàng)建,屬性的創(chuàng)建等。當(dāng)然在此也有必要說明一下為什么采用后者,因為后者符合XML DOM標(biāo)準(zhǔn),有利于你認(rèn)識dom模型,并且構(gòu)造速度更快,構(gòu)造出的xml文檔更不容易出錯,其中有一些細(xì)節(jié)我也會做一些必要的講述。
?
主方法如下:
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é)點
???? domDoc.AppendChild(root);
?
???? XmlElement chnode = domDoc.CreateElement("channel");
???? root.AppendChild(chnode);
?
???? XmlElement element = domDoc.CreateElement("title");
???? XmlNode textNode = domDoc.CreateTextNode("搜狐焦點新聞");??? //文本結(jié)點
???? 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é)點
???? XmlNode cDataNode = domDoc.CreateCDataSection("即時報道國內(nèi)外時政大事,解讀環(huán)球焦點事件");
???? element.AppendChild(cDataNode);
???? chnode.AppendChild(element);
?
???? DataTable dt = GetDataTab();???? //訪問數(shù)據(jù)庫,獲取要在rss中顯示的記錄
?
???? foreach(DataRow dr in dt.Rows)
???? {
???????? element = domDoc.CreateElement("item");
?
???????? //...
???????? //創(chuàng)建內(nèi)容結(jié)點,常見的如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部分是為說明實例手工添加):
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<channel>
<title>搜狐焦點新聞</title>
<link>http://www.sohu.com</link>
<description>
<![CDATA[即時報道國內(nèi)外時政大事,解讀環(huá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衛(wèi)93萬元/套</title>
?<link>http://www.ewhouse.com/HouseInfo.aspx?publishId=3440</link>
?<pubDate>2006-10-12 10:50:18</pubDate>
?</item>
</channel>
</rss>
有幾點值得說明的有:
1、?
CreateTextNode,即創(chuàng)建文本結(jié)點
有人習(xí)慣使用InnerText來添加結(jié)點中的文本,雖然結(jié)果是一樣的,但是要知道在DOM中文本也是結(jié)點,既然要符合DOM標(biāo)準(zhǔn),就要進(jìn)行到底!
2、?
輸出
我在實例中使用XmlTextWriter輸出。
實際還可以使用如下:
Response.ContentType = "application/xml"; // 輸出并按xml數(shù)據(jù)顯示
Response.Write(domDoc.InnerXml);
但是,使用XmlTextWriter輸出更快,所以也建議使用這個方法。
?
用XMLTextWriter方法實現(xiàn)如下:
XmlTextWriter writer = new XmlTextWriter(this.Response.OutputStream,System.Text.Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.Indentation = 3;
writer.WriteStartDocument();
writer.WriteComment("Create using XmlTextWriter at " + DateTime.Now);
writer.WriteStartElement("rss");
writer.WriteAttributeString("version","2.0");
writer.WriteStartElement("channel");
writer.WriteElementString("title","搜狐焦點新聞");
writer.WriteElementString("link","http://www.sohu.com");
writer.WriteCData("即時報道國內(nèi)外時政大事,解讀環(huán)球焦點事件");
//
//中間添加訪問數(shù)據(jù)庫部分...
//
writer.WriteEndElement();
writer.WriteEndElement();
writer.Flush();
writer.Close();
這個方法是把xml文件輸出 ,如果要保存為xml文件,第一句用這樣:
XmlTextWriter writer = new XmlTextWriter(Server.MapPath("grade.xml",null);??
?
總結(jié)
以上是生活随笔為你收集整理的C#与RSS亲密接触的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。