Question

I'm trying to get the innerHTML from a div but I need to ignore the divs inside it that have a specific id.

In the bellow sample, I need to get all #data innerHtml but ignore div#ignoreme.

<div id="data">
    <div id="ignoreme">ignore</div>
    <p>line</p>
    this is another line
</div>

I tried doc.DocumentNode.SelectSingleNode("//*[@id='data']").SelectNodes("//*[not(@id='ignoreme')]");

But it isn't working, this always returns the full html document (!?)

So, is this possible with html agility pack, XPath?

Was it helpful?

Solution

Try this :

var sol = doc.DocumentNode.SelectSingleNode("//*[@id='data']")
           .SelectNodes(".//*[not(@id='ignoreme')]").ToList();

Or you can do this instead:

var sol1 = doc.DocumentNode.SelectSingleNode("//*[@id='data']")
                .Descendants()
                .Where(p => p.Id != "ignoreme")
                .ToList();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top