开发者

check for null with HtmlAgilityPack

开发者 https://www.devze.com 2023-02-22 20:16 出处:网络
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.

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消