如何提取 Atom/RSS
给定一个 URL,如果它有任何 RSS 节点,那么我将添加到数据库中。
例如:
对于 此 URL,rssDoc.SelectNodes("rss/channel/ item").Count
大于零。
但对于atom 网址, rssDoc.SelectNodes("rss/channel/item").count
等于零。
如何检查 Atom/RSS URL 是否有节点?我尝试过 rssDoc.SelectNodes("feed/entry").Count ,但计数为零。
Public Shared Function HasRssItems(ByVal url as string) As Boolean
Dim myRequest As WebRequest
Dim myResponse As WebResponse
Try
myRequest = System.Net.WebRequest.Create(url)
myRequest.Timeout = 5000
myResponse = myRequest.GetResponse()
Dim rssStream As Stream = myResponse.GetResponseStream()
Dim rssDoc As New XmlDocument()
rssDoc.Load(rssStream)
Return rssDoc.SelectNodes("rss/channel/item").Count > 0
Catch ex As Exception
Return False
Finally
myResponse.Close()
End Try
结束功能
Given a URL, if it has any RSS nodes, then I am adding to the database.
e.g.:
For this URL, rssDoc.SelectNodes("rss/channel/item").Count
is greater than zero.
But for the atom url, rssDoc.SelectNodes("rss/channel/item").count
is equal to zero.
How can I check if the Atom/RSS URL has any nodes or not? I have tried for rssDoc.SelectNodes("feed/entry").Count
, but is giving me zero count.
Public Shared Function HasRssItems(ByVal url as string) As Boolean
Dim myRequest As WebRequest
Dim myResponse As WebResponse
Try
myRequest = System.Net.WebRequest.Create(url)
myRequest.Timeout = 5000
myResponse = myRequest.GetResponse()
Dim rssStream As Stream = myResponse.GetResponseStream()
Dim rssDoc As New XmlDocument()
rssDoc.Load(rssStream)
Return rssDoc.SelectNodes("rss/channel/item").Count > 0
Catch ex As Exception
Return False
Finally
myResponse.Close()
End Try
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的主要问题是这一行的 XML“节点路径”:
Return rssDoc.SelectNodes("rss/channel/item").Count > 0
仅对 RSS feed 有效,对 ATOM 提要。
我过去解决这个问题的一种方法是使用一个简单的函数将 ATOM 提要转换为 RSS 提要。当然,您可以采用另一种方式,或者根本不转换,但是,转换为单一格式使您能够编写一个“通用”代码块,该代码块将提取您可能感兴趣的提要项目的各种元素(即日期、标题等)
有一篇 ATOM 到 RSS 转换器文章 然而,在提供此类转换的代码项目上,这是在 C# 中。我之前自己手动将其转换为 VB.NET,因此这是 VB.NET 版本:
用法相当简单。只需将您的 ATOM feed 加载到
XmlDocument
对象中并将其传递给此函数,您就会得到一个 RSS 格式的XmlDocument
对象!如果您有兴趣,我已将整个RSSReader 类放在pastebin.com 上
Your main problem here is that the XML "node path" on this line:
Return rssDoc.SelectNodes("rss/channel/item").Count > 0
is only valid for RSS feeds, not ATOM feeds.
One way I've got over this in the past is to use a simple function to convert an ATOM feed into an RSS feed. Of course, you could go the other way, or not convert at all, however, converting to a single format enables you to write one "generic" chunk of code that will pull out the various elements of a feed's items that you may be interested in (i.e. date, title etc.)
There is an ATOM to RSS Converter article on Code Project that provides such a conversion, however, that is in C#. I have previously manually converted this to VB.NET myself, so here's the VB.NET version:
Usage is fairly straight forward. Simply load in your ATOM feed into an
XmlDocument
object and pass it to this function, and you'll get anXmlDocument
object back, in RSS format!If you're interested, I've put an entire RSSReader class up on pastebin.com