惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
aimingoo的专栏
aimingoo的专栏
IT之家
IT之家
G
Google Developers Blog
爱范儿
爱范儿
博客园 - 司徒正美
Recent Announcements
Recent Announcements
The Register - Security
The Register - Security
J
Java Code Geeks
The Cloudflare Blog
M
MIT News - Artificial intelligence
Apple Machine Learning Research
Apple Machine Learning Research
Microsoft Security Blog
Microsoft Security Blog
博客园 - Franky
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
Blog — PlanetScale
Blog — PlanetScale
Vercel News
Vercel News
宝玉的分享
宝玉的分享
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
B
Blog
小众软件
小众软件
Microsoft Azure Blog
Microsoft Azure Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
T
Troy Hunt's Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
H
Hacker News: Front Page
H
Help Net Security
S
Security @ Cisco Blogs
V
V2EX
Security Archives - TechRepublic
Security Archives - TechRepublic
Stack Overflow Blog
Stack Overflow Blog
O
OpenAI News
L
LINUX DO - 最新话题
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
S
Secure Thoughts
Help Net Security
Help Net Security
F
Full Disclosure
博客园 - 叶小钗
The Hacker News
The Hacker News
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Jina AI
Jina AI
K
Kaspersky official blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Vulnerabilities – Threatpost
P
Privacy International News Feed
Scott Helme
Scott Helme

博客园 - Sherrys

Content-Type 参数 Sql 2000 中行转列的查询方法 DotNet Framework 小技巧 通过 INotifyPropertyChanged 实现观察者模式 使用 .NET 2.0 SecureString 类保护敏感数据 API参数说明符前缀详解 C#中调用Windows API的要点 - Sherrys - 博客园 SQL 处理简单的 Xml Special Considerations When Using Query Notifications SQL远程连接 SQL 事务的隔离 SELECT 语句收藏(2000 & 2005) 自动处理 SQL 2005 表格数据 SQL事务的使用 用JS让文章内容指定的关键字加亮 - Sherrys - 博客园 查询SQL连接数的方法 对象的继承方案 使用 ICallbackEventHandler aspx页面中的DropDownList 的 SelectValue 出现中文导致不回调方法的问题 如何利用SQL Server 2005数据库快照形成报表
利用.NET Framework使用RSS feed
Sherrys · 2007-05-08 · via 博客园 - Sherrys

链接到服务器

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

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

Listing A

//Create a WebRequest object

WebRequest myRequest = WebRequest.Create("http://rss-feeds.msn.com/autos/autosnews.xml");

在这个例子中,也可以用完整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格式与这个格式不同。如果你想获得RSS格式的更多信息,可以查看O'Reilly的摘要。

一个完整的例子:

//Create a WebRequest object

WebRequest myRequest = WebRequest.Create("http://rss-feeds.msn.com/autos/autosnews.xml");

//Get the response from the WebRequest

WebResponse myResponse = myRequest.GetResponse();

//Get the response's stream

Stream rssStream = myResponse.GetResponseStream();

//Create the Xml Document

XmlDocument feedDocument = new XmlDocument();

//Load the stream into the XmlDocument object.

feedDocument.Load(rssStream);

//Create a XmlNamespaceManager for our namespace.

XmlNamespaceManager manager =

new XmlNamespaceManager(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;

Console.WriteLine(title);

Console.WriteLine(link);

Console.WriteLine(description);

}

Console.ReadLine();