0
<html>
<body>
    <p> This 
        <p> should not be displayed twice</p>
    </p>
    <a href="http://www.w3schools.com">Visit W3Schools.com!</a> 
    <div> Do not enter</div>
    <p> Gibberish </p>
</body>
</html>

So I want to access certain first child nodes of the body-tag. In this case I only want p-tag and a-tag.

Current code:

foreach(HtmlNode h in body.Elements("p"))
{
    if (h.Name == "p"){
        //Do something
    }
    if (h.Name == "a"){
       //Do something else
    }
}

Obviously this doesn't work since I only get the p-tags from the body-tag. However is there some awesome xpath code that can get me the a-tags aswell.

Kent Kostelac
  • 2,257
  • 3
  • 29
  • 42

1 Answers1

1

You can use an "or" (|) in your xpath. So you'd come out looking something like this:

String xPath = "p|a";
HtmlNodeCollection bodyDecendants = bodyNode.SelectNodes(xPath);
Matt
  • 26
  • 4
  • Already tried that. That didn't work. It's like HAP doesn't fully support Xpaths. But apparently it works perfectly with SelectNodes. – Kent Kostelac Sep 29 '15 at 14:51