Retrieve an attribute with XPath

Topics: Developer Forum
Oct 28, 2013 at 2:45 PM
I've got the following code snippet and would like to retrieve the value of an attribute by including it in the XPath expression:
foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//a/@href"))
{
    HtmlAttribute attr = node as HtmlAttribute;
    if (attr != null)
    {
        return attr.Value;
    }
}
But the compiler complains it can never cast an HtmlNode to an HtmlAttribute. With XML, that should work just fine IIRC. How does it work with your Html* classes?