当前位置:首页>网络学院>程序开发>ASP.NET教程>文章内容

如何利用.NET Framework使用RSS feed

[ 来源:www.it55.com | 作者: | 时间:2007-07-15 | 收藏 | 推荐 ] 【

  如果想利用.NET Framework来使用RSS feed的话,这其实并不复杂。你只需要做下面几步就可以了: it55.com

  ◆链接到提供RSS feed的网站

http://www.it55.com/

  ◆下载feed XML

45398 www.it55.com it55学习IT知识,享受IT生活 4dfkjn

  ◆将feed的XML装载到允许搜索的对象中 it55.com

  ◆为你想提取的结点搜索feed的XML

http://www.it55.com/

  .NET Framework提供了内置函数来完成所有的任务。我们所需要做的就是,将这些功能绑定在一起,这样我们就可以使用RSS feeds。 45398 www.it55.com it55学习IT知识,享受IT生活 4dfkjn

  链接到服务器

sflj www.it55.com kg^&fgd

  我们可以使用WebRequest对象链接到服务器上。WebRequest对象使你可以在Web站点上贴出请求,自从RSS通过HTTP传输后,WebRequest对象就成了链接服务器最主要的选择了。

www.it55.com

  Listing A中的代码告诉我们,任何将一个新的WebRequest对象与一个URL进行连接。

sflj www.it55.com kg^&fgd

  Listing A

http://www.it55.com/

//Create a WebRequest object

WebRequest myRequest = WebRequest.Create(url);

sflj www.it55.com kg^&fgd

www.it55.com

  在这个例子中,也可以用完整URL的来取代RSS feed中的“url”。下面是MSN Automotive RSS feed的地址:http://rss-feeds.msn.com/autos/autosnews.xml www.it55.com

  下载RSS数据 免费资源www.it55.com

  当我们连接到服务器之后,我们需要下载feed提供的数据。WebRequest对象为实现这个目的提供了一个GetResponse()方法。WebRequest.GetResponse()方法返回一个WebRequest对象,这个对象根据我们的请求给我们访问服务器的响应。 it55.com

  在这里我们将用到WebResponse(Web响应)对象的GetResponseStream()方法。这个方法返回一个Stream对象,这个对象中包含了服务器所响应的原始RSS XML。Listing B中的代码告诉我们如何从WebRequest(Web请求)对象得到WebResponse(Web响应)对象,和如何从WebResponse(Web响应)对象得响应流。 http://www.it55.com/

  Listing B vd;k;l www.it55.com rdfg

//Get the response from the WebRequest

WebResponse myResponse = myRequest.GetResponse();

//Get the response's stream

Stream rssStream = myResponse.GetResponseStream(); 免费资源www.it55.com

  将RSS数据装载到XML文档中 sflj www.it55.com kg^&fgd

  一旦我们从WebResponse(Web响应)对象得到了流,我们就将这个流下载到XmlDocument对象中了。这样我们就很容易对XML数据进行分析了,并能轻松地从中取值。得到XmlDocument装载Stream最简单的方法是,创建一个新的XmlDocument对象,并将我们的Stream传递给Load方法。Listing C为我们说明了这个方法的使用。 vd;k;l www.it55.com rdfg

  Listing C it55.com

//Create the Xml Document

XmlDocument document = newXmlDocument();

//Load the stream into the XmlDocument object.

document.Load(rssStream);
it55.com

  分析XML

www.it55.com在线教程

  这是使用RSS feed最难的部分。我们必须使用刚才创建的XmlDocument来得到含有我们自己数据的XML结点。我们普遍感兴趣的结点是:

免费资源www.it55.com

  ◆Feed的标题,它存放在feed XML中的/rss/channel/title文件里面

  ◆Feed的文章,它存放在feed XML中的/rss/channel/item文件里面。在这个位置可能有多个结点。

  ◆文章的标题,它存放在文章结点中的title里面。

  ◆文章的描述,它存放在文章结点的description里面。

  ◆文章的链接,它存放在文章结点的link里面。 vd;k;l www.it55.com rdfg

  我们可以使用XmlDocument对象内置的SelectSingleNode函数和SelectNodes函数来得到这些结点。这两个函数都可以接受XPath查询,也都可以返回与查询结果相匹配的一个或多个结点。 www.it55.com

  Listing D这段代码告诉我们如何使用XmlDocument和Xpath从RSS feed中分析出每个单独的元素。

www.it55.com

  Listing D

vd;k;l www.it55.com rdfg

//Get an XmlDocument object that contains the feed's XML

XmlDocument feedDocument =

GetXmlDocumentFromFeed("http://rss-feeds.msn.com/autos/autosnews.xml"); vd;k;l www.it55.com rdfg

//Create a XmlNamespaceManager for our namespace.

XmlNamespaceManager manager =

newXmlNamespaceManager(feedDocument.NameTable);

IT资讯之家 www.it55.com

//Add the RSS namespace to the manager.

manager.AddNamespace("rss", "http://purl.org/rss/1.0/");

IT资讯之家 www.it55.com

//Get the title node out of the RSS document

XmlNode titleNode =

feedDocument.SelectSingleNode("/rss/channel/title", manager);

vd;k;l www.it55.com rdfg

//Get the article nodes

XmlNodeList articleNodes =

feedDocument.SelectNodes("/rss/channel/item", manager);

sflj www.it55.com kg^&fgd

//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;

vd;k;l www.it55.com rdfg

//Get the article's link

string link =

articleNode.SelectSingleNode("link", manager).InnerText;

www.it55.com

//Get the article's description

string description =

articleNode.SelectSingleNode("description", manager).InnerText;

}

45398 www.it55.com it55学习IT知识,享受IT生活 4dfkjn

www.it55.com在线教程

  不是所有的RSS feed的创建都是相同的

http://www.it55.com/

  如果所有的RSS feed都使用相同的格式,它将变得更强大,然而RSS feed有许多不同的版本和实现。在这篇文章中描述的格式适合大部分的feed,可能有少部分的RSS feed格式与这个格式不同。

http://www.it55.com/


(编辑:IT资讯之家 www.it55.com

返回顶部
 

网友评论

[以下评论为网友观点,不代表本站。请自觉遵守互联网相关政策法规,所有连带责任均有评论者自负。]
[不超过250字]

图片文章