htmlagilitypack选择所有节点 [英] htmlagilitypack selecting all node

查看:285
本文介绍了htmlagilitypack选择所有节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码

i have this code

Dim htmldoc As HtmlDocument = New HtmlDocument()
htmldoc.LoadHtml(strPageContent)
Dim root As HtmlNode = htmldoc.DocumentNode

For Each link As HtmlNode In root.SelectNodes("//a")
    If link.HasAttributes("href") Then doSomething() ''this doesn''t work because hasAttributes only checks whether an element has attributes or not
Next



但出现错误 Object reference not set to an instance of an object.

该文档包含至少一个锚标签?如何检查属性是否存在?

我尝试了这个link.HasAttributes("title")并遇到了另一个错误

Public ReadOnly Property HasAttributes() As Boolean'' has no parameters and its return type cannot be indexed.

Thanks



but am getting an error Object reference not set to an instance of an object.

the document contains at least one anchor-tag? how do i check if an attribute exits?

i tried this link.HasAttributes("title") and get another error

Public ReadOnly Property HasAttributes() As Boolean'' has no parameters and its return type cannot be indexed.

thanks

推荐答案

您的代码做出了巨大的假设.发生错误是因为您假设root.SelectNodes("//a")实际上正在返回Node的集合.在尝试使用此返回值之前,您可能需要检查它是否为null.

HasAttributes属性没有任何参数,因此我不知道您要通过传入一个参数来尝试做什么.您可能想阅读HtmlNode.HasAttibutes的文档并了解它的作用,然后再尝试将其用于某些用途.
Your code is making a huge assumption. The error occurs because you''re assuming that root.SelectNodes("//a") is actually returning a collection of Nodes. You might want to check this return for null before you try and use it.

The HasAttributes property doesn''t take any parameters, so I have no idea what you''re trying to do by passing in one. You might want to read up on the documentation for HtmlNode.HasAttibutes and understand what it does before you try and use it for something.


这篇关于htmlagilitypack选择所有节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆