-
-
Notifications
You must be signed in to change notification settings - Fork 381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MoveChild doesn't set parent of childs correctly #574
Comments
Hello @dartrax , The method The I don't know if that's a bug or if this works as intended by the original author (I would have expected the same behavior as you). What I would recommend you is probably this code: public void InsertHtmlRootElement(HtmlDocument doc)
{
HtmlNode HtmlTag = doc.DocumentNode.ChildNodes.FindFirst("html");
if (HtmlTag is null)
{
// Create new <html></html> Node
HtmlTag = new HtmlNode(HtmlNodeType.Element, doc, 0) { Name = "html" };
var childNodes = doc.DocumentNode.ChildNodes.ToList();
childNodes.ForEach(x => doc.DocumentNode.RemoveChild(x));
// Append new HTML Node into Document
doc.DocumentNode.AppendChild(HtmlTag);
// Move all childs from root into the new HTML Node
childNodes.ForEach(x => HtmlTag.AppendChild(x));
}
} All childs get removed from the document and after added to the HTML node. Best Regards, Jon |
Hi Jon, I would suggest to add the Ideally, of course this use case should be added to make it work in both cases ;-) |
Thank you for your suggestion. Both methods have been added with a note about the "different document": https://html-agility-pack.net/traversing Unfortunately, .NET Fiddle is currently down so I was not able to add the example Best Regards, Jon |
Let's say I have an HtmlDocument that has some child nodes in DocumentNode like
<div></div><div></div><div></div>
. I want to encapsulate the existing childs into a new<html></html>
Node:This works but all children of HtmlTag have their ParentNode set to null. I guessed that I first need to add the HtmlTag to the document and then insert the childs to fix it:
As it turns out, the children still have no parent. To work around that, I have to call
c.SetParent(HtmlTag);
explicitly.The text was updated successfully, but these errors were encountered: