xml - HtmlAgilityPack仅返回第一个匹配项

标签 xml vb.net youtube html-agility-pack

<entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:yt="http://gdata.youtube.com/schemas/2007">
    <id>http://gdata.youtube.com/feeds/api/users/upvZG-5ko_eiXAupbDfxWw</id>
    <published>2005-10-02T16:06:36.000Z</published>
    <updated>2014-05-29T20:18:58.000Z</updated>
    <category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#userProfile"/>
    <category scheme="http://gdata.youtube.com/schemas/2007/channeltypes.cat" term="DIRECTOR"/>
    <title type="text">CNN</title>
    <content type="text">CNN operates as a division of Turner Broadcasting System, which is a subsidiary of Time Warner. CNN identifies itself as -- and is widely known to be - the most trusted source for news and information. The CNN umbrella includes nine cable and satellite television networks, two radio networks, the CNN Digital Network, which is the top network of news Web sites in the United States, and CNN Newsource, the world's most extensively syndicated news service. CNN is proud of our ability to bring you up-to-the-minute news from around the world, as a result of our many extensions.</content>
    <link rel="alternate" type="text/html" href="http://www.youtube.com/channel/UCupvZG-5ko_eiXAupbDfxWw"/>
    <link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/upvZG-5ko_eiXAupbDfxWw"/>
    <author>
        <name>CNN</name>
        <uri>http://gdata.youtube.com/feeds/api/users/CNN</uri>
    </author>
    <gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.subscriptions" href="http://gdata.youtube.com/feeds/api/users/cnn/subscriptions" countHint="6"/>
    <gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.contacts" href="http://gdata.youtube.com/feeds/api/users/cnn/contacts" countHint="4250"/>
    <gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.inbox" href="http://gdata.youtube.com/feeds/api/users/cnn/inbox"/>
    <gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.playlists" href="http://gdata.youtube.com/feeds/api/users/cnn/playlists"/>
    <gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.uploads" href="http://gdata.youtube.com/feeds/api/users/cnn/uploads" countHint="58269"/>
    <gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.newsubscriptionvideos" href="http://gdata.youtube.com/feeds/api/users/cnn/newsubscriptionvideos"/>
    <yt:location>US</yt:location>
    <yt:statistics lastWebAccess="1970-01-01T00:00:00.000Z" subscriberCount="484272" videoWatchCount="0" viewCount="0" totalUploadViews="400764857"/>
    <media:thumbnail url="http://i1.ytimg.com/i/upvZG-5ko_eiXAupbDfxWw/1.jpg?v=51df0b06"/>
    <yt:username>cnn</yt:username>
</entry>

我尝试了这段代码,但只返回了第一个匹配项,我需要countHint的值等于 58269
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.uploads" href="http://gdata.youtube.com/feeds/api/users/cnn/uploads" countHint="58269"/>

我的代码:
Dim request As HttpWebRequest = WebRequest.Create("http://gdata.youtube.com/feeds/api/users/cnn")
Dim response As HttpWebResponse = request.GetResponse()
Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
Dim page As String = reader.ReadToEnd()
Dim dokuman = New HtmlAgilityPack.HtmlDocument()
dokuman.LoadHtml(page)
Dim gets As HtmlAgilityPack.HtmlNodeCollection = dokuman.DocumentNode.SelectNodes("//entry")
For Each node In gets
    If node.ChildNodes("gd:feedLink").Attributes("href").Value = "http://gdata.youtube.com/feeds/api/users/cnn/uploads" Then
        '......
    End If
Next

如何通过HtmlAgiltyPack获得 58269 的值?如果我尝试
dokuman.DocumentNode.SelectNodes("//gd:feedLink") 

然后抛出一个错误!

最佳答案

看来HtmlAgilityPack doesn't support XPath containing prefix。您可以通过选择<entry>元素的所有子代来解决此限制,然后使用LINQ按节点名缩小结果范围:

Dim result As HtmlNode = _
        dokuman.DocumentNode _
           .SelectNodes("//entry/*") _
           .FirstOrDefault(Function(x) _
                               x.Name = "gd:feedlink" AndAlso _
                               x.Attributes("href").Value = "http://gdata.youtube.com/feeds/api/users/cnn/uploads")
If Not result Is Nothing Then
    'this is a better practice for accessing attribute value from HtmlNode...'
    '...to avoid exception if such attribute not found'
    Console.WriteLine(result.GetAttributeValue("countHint", "not found"))
End If

关于xml - HtmlAgilityPack仅返回第一个匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23956098/

相关文章:

java - 如何在 Activiti 进程中从网关调用 Java 方法

ajax - 对 OPTIONS 的 POST 更改 + 对预检请求的响应未通过访问控制检查 : No 'Access-Control-Allow-Origin' header

asp.net - 我的数据库表未排序

.net - 如何在不使用日期时间控件的情况下仅计算小时和分钟差异?

vb.net - 如何将消失标签编程到 ASP 文本框中?

html - 如何获取自动播放YouTube视频的嵌入式代码

java - 使用从 XML 文件中提取的数据创建正则表达式

java - Android 中无法使用 JAVA 解决异常处理程序?

youtube - Windows Phone 7 是否有类似于 iPhone 的 YouTube 播放器?

javascript - 隐藏时停止在后台自动播放Youtube视频