如果想利用.net framework來使用rss feed的話,這其實并不復雜。你只需要做下面幾步就可以了:
◆鏈接到提供rss feed的網站
◆下載feed xml
◆將feed的xml裝載到允許搜索的對象中
◆為你想提取的結點搜索feed的xml
.net framework提供了內置函數來完成所有的任務。我們所需要做的就是,將這些功能綁定在一起,這樣我們就可以使用rss feeds。
鏈接到服務器
我們可以使用webrequest對象鏈接到服務器上。webrequest對象使你可以在web站點上貼出請求,自從rss通過http傳輸后,webrequest對象就成了鏈接服務器最主要的選擇了。
listing a中的代碼告訴我們,任何將一個新的webrequest對象與一個url進行連接。
listing a
//create a webrequest object
webrequest myrequest = webrequest.create(url);
在這個例子中,也可以用完整url的來取代rss feed中的“url”。下面是msn automotive rss feed的地址:http://rss-feeds.msn.com/autos/autosnews.xml
下載rss數據
當我們連接到服務器之后,我們需要下載feed提供的數據。webrequest對象為實現這個目的提供了一個getresponse()方法。webrequest.getresponse()方法返回一個webrequest對象,這個對象根據我們的請求給我們訪問服務器的響應。
在這里我們將用到webresponse(web響應)對象的getresponsestream()方法。這個方法返回一個stream對象,這個對象中包含了服務器所響應的原始rss xml。listing b中的代碼告訴我們如何從webrequest(web請求)對象得到webresponse(web響應)對象,和如何從webresponse(web響應)對象得響應流。
listing b
//get the response from the webrequest
webresponse myresponse = myrequest.getresponse();
//get the response's stream
stream rssstream = myresponse.getresponsestream();
將rss數據裝載到xml文檔中
一旦我們從webresponse(web響應)對象得到了流,我們就將這個流下載到xmldocument對象中了。這樣我們就很容易對xml數據進行分析了,并能輕松地從中取值。得到xmldocument裝載stream最簡單的方法是,創建一個新的xmldocument對象,并將我們的stream傳遞給load方法。listing c為我們說明了這個方法的使用。
listing c
//create the xml document
xmldocument document = newxmldocument();
//load the stream into the xmldocument object.
document.load(rssstream);
分析xml
這是使用rss feed最難的部分。我們必須使用剛才創建的xmldocument來得到含有我們自己數據的xml結點。我們普遍感興趣的結點是:
◆feed的標題,它存放在feed xml中的/rss/channel/title文件里面
◆feed的文章,它存放在feed xml中的/rss/channel/item文件里面。在這個位置可能有多個結點。
◆文章的標題,它存放在文章結點中的title里面。
◆文章的描述,它存放在文章結點的description里面。
◆文章的鏈接,它存放在文章結點的link里面。
我們可以使用xmldocument對象內置的selectsinglenode函數和selectnodes函數來得到這些結點。這兩個函數都可以接受xpath查詢,也都可以返回與查詢結果相匹配的一個或多個結點。
listing d這段代碼告訴我們如何使用xmldocument和xpath從rss feed中分析出每個單獨的元素。
listing d
//get an xmldocument object that contains the feed's xml
xmldocument feeddocument =
getxmldocumentfromfeed("http://rss-feeds.msn.com/autos/autosnews.xml");//create a xmlnamespacemanager for our namespace.
xmlnamespacemanager manager =
newxmlnamespacemanager(feeddocument.nametable);//add the rss namespace to the manager.
manager.addnamespace("rss", "http://purl.org/rss/1.0/");//get the title node out of the rss document
xmlnode titlenode =
feeddocument.selectsinglenode("/rss/channel/title", manager);//get the article nodes
xmlnodelist articlenodes =
feeddocument.selectnodes("/rss/channel/item", manager);//loop through the articles and extract
// their data.
foreach (xmlnode articlenode in articlenodes)
{
//get the article's title.
string title =
articlenode.selectsinglenode("title", manager).innertext;//get the article's link
string link =
articlenode.selectsinglenode("link", manager).innertext;//get the article's description
string description =
articlenode.selectsinglenode("description", manager).innertext;
}
不是所有的rss feed的創建都是相同的
如果所有的rss feed都使用相同的格式,它將變得更強大,然而rss feed有許多不同的版本和實現。在這篇文章中描述的格式適合大部分的feed,可能有少部分的rss feed格式與這個格式不同。
新聞熱點
疑難解答
圖片精選