Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8210579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:07:08+00:00 2026-06-07T10:07:08+00:00

Here is my simplified HTML: <html> <body> <div id=mainDiv> <div id=divToRemove></div> <div id=divToKeep></div> <div

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:07:10+00:00Added an answer on June 7, 2026 at 10:07 am

    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.

    var divs = htmldoc.DocumentNode.SelectNodes("//div");
    if (divs != null)
    {
        foreach (var tag in divs)
        {
            if (tag.Attributes["class"] != null && string.Compare(tag.Attributes["class"].Value, "divToRemove", StringComparison.InvariantCultureIgnoreCase) == 0)
            {
                tag.Remove();
            } else if(tag.Attributes["id"] != null && string.Compare(tag.Attributes["id"].Value, "divToRemove", StringComparison.InvariantCultureIgnoreCase) == 0) {
                tag.Remove();
            }
        }
    }
    

    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…

    var mainDiv = htmldoc.DocumentNode.SelectSingleNode("//div[@id='mainDiv']");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an html table. Here is a simplified version: <table> <tr> <td><div style=display:
I want to list all users with their corropsonding user class. Here are simplified
I've got this html (simplified) <div class=outer style=float:left> <div class=inner style=float:left> <div class=contained style=clear:both>...</div>
A simplified version of problem I am experiencing: Here is my HTML form: <form
Here's what I'm trying to do, simplified: <input rel=TimeStart> // bunch of HTML code
Here is simplified version of my requirement I have a java class say Processor
I have a following HTML/CSS (simplified): <body> <style> body { margin: 0px; padding: 0px;
I have simplified my page and here what I have: <html> <head> <meta http-equiv=content-type
Here is a simplified version of my page: <html xmlns=http://www.w3.org/1999/xhtml> <head runat=server> <title></title> <script
Here's the simplified html: <!DOCTYPE html> <html> <head> <script type=text/javascript> function handle() { console.log(fired);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.