Here is my simplified HTML:
<html>
<body>
<div id="mainDiv">
<div id="divToRemove"></div>
<div id="divToKeep"></div>
<div class="divToRemove"></div>
<div class="divToRemove"></div>
</div>
</body>
</html>
I want to remove the divs with ID or class named “divToRemove”
and then I want to select only the div called “mainDiv” (in a HtmlNode).
The results should be:
<div id="mainDiv">
<div id="divToKeep"></div>
</div>
How can i do that using Html Agility Pack?
Thanks!
The following code is a adapted from this Html Agility Pack forum page to fit your needs. Essentially, we will grab all divs and then loop through them and check their class or their id for a match. If it’s there remove it.
You can also combine these if statements into one large if statement, but I thought this read better for the answer.
Finally, select the node you were looking for…