mpen mpen - 7 months ago 45
C# Question

HtmlAgilityPack HasAttribute?

All I want to do is


But if the node doesn't have the
attribute, it crashes. So, I have to check for its existence first, right? How do I do that?
is not a dict (its a list that contains an internal dict??), and there's no HasAttribute method (just a HasAttributes which indicates if it has any attribute at all). What do I do?


Try this:

String val;
if(node.Attributes["class"] != null)
  val = node.Attributes["class"].Value;

Or you might be able to add this

public static class HtmlAgilityExtender
    public static String ValueOrDefault(this HtmlAttribute attr)
        return (attr != null) ? attr.Value : String.Empty;

And then use


I havent tested that one, but it should work.