精彩专题推荐:建站之入门课 建站之必修课 建站之关键课 网站价值所在 流量提高专题 css+div 标准 个人网站打造全过程
返回建站学首页
导航:
建站首页 | 网站设计 | 网站开发 | 网站运营 | 网页软件 | 建站指南 | 搜索优化 | 图像处理 | 视频教程 | 书籍教程 | 建站专题
当前位置:首页>网站开发>asp.net教程>正文

如何利用.NET Framework使用RSS feed


来源:网络 时间:07-05-18 点击: 点击这里收藏本文

  我们可以使用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格式与这个格式不同。

9 7 3 1 2 4 8 :

  把此文章收藏到:          
广而告之
文章搜索
  • Google JZxue.Com

关于我们 | 联系我们 | 友情链接 | 网站地图
Copyright © 2005 - 2006 建站学 All rights reserved.