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 9205697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:00:43+00:00 2026-06-18T00:00:43+00:00

When I remove a node (with keepGrandChildren ) that has both text and child

  • 0

When I remove a node (with keepGrandChildren) that has both text and child nodes inside, the text is pushed all the way after the child nodes, rather than stay in its original location.

Example:

var doc = new HtmlDocument();
doc.LoadHtml(@"
<span id='first'>
    This text comes first.
    <span id='second'>This text comes second.</span>
</span>");

var node = doc.GetElementbyId("first");
node.ParentNode.RemoveChild(node, true);
doc.Save(Console.Out);

The output that I get is:

    <span id='second'>This text comes second.</span>
        this text comes first.

Instead of:

    this text comes first.
    <span id='second'>This text comes second.</span>

Is there any way of removing a node using keepGrandChildren without the text inside getting pushed towards the end?
I want to conserve absolute order and make sure that no text or nodes change their original position, otherwise the document will be ruined.

Edit:
I’m using HtmlAgilityPack 1.4.6.0 and .NET 4.0

  • 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-18T00:00:44+00:00Added an answer on June 18, 2026 at 12:00 am

    It is a know issue in the HtmlAgilityPack. The following code should solve the problem:

    public static void RemoveChildKeepGrandChildren(HtmlNode parent, HtmlNode oldChild)
    {
        if (oldChild.ChildNodes != null)
        {
            HtmlNode previousSibling = oldChild.PreviousSibling;
            foreach (HtmlNode newChild in oldChild.ChildNodes)
            {
                parent.InsertAfter(newChild, previousSibling);
                previousSibling = newChild;  // Missing line in HtmlAgilityPack
            }
        }
        parent.RemoveChild(oldChild);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there a way to remove a node that is wrapping a few other
What would be the preferred way to remove all empty and unnecessery nodes? For
remove() method without any argument removes all documents inside the collection. $this->db->$collection->remove(); But how
I'm having some trouble finding all nodes accessible from a given node. Barring the
I need to empty a collection, removing each item in order. this.nodes.each(function(node){ this.nodes.remove(node); },
The standard way to remove a node using PHP's DOMDocument is: $dom = dom_import_simplexml($node);
In a C# Treeview, I need to remove all branches that do not contain
In Neo4j, if I delete an indexed node do I have to remove that
I'm trying to select all elements that have a given class and remove them
i need to remove xmi node if there is only 1 child. If i

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.