check for null with HtmlAgilityPack
How do you check for null with HtmlAgilityPack? I'm getting "Property or indexer 'HtmlAgilityPack.HtmlNode.HasChildNodes' cannot be assigned to -- it is read only" with the following.
if (Node.Element("TD").HasChildNodes = DBNull.Value)
I"m getting "Object reference not s开发者_C百科et to an instance of an object. " with
if (Node.Element("TD").HasChildNodes)
First, the =
operator is the assignment operator, not the comparison operator (==
). In your first example you are trying to assign DBNull.Value
to HasChildeNodes
, a read-only property, not compare it to a value.
Second, you don't test against DBNull.Value
, but against null
. DBNull.Value
is to be used when testing values of items returned by the database using ADO.NET. For all other cases, you should use null
.
So, the test should be:
if (Node.Element("TD").HasChildNodes == null)
精彩评论