How to substitude/add root element in HtmlAgilityPack?

c# html html-agility-pack

Question

Let's say I have the HTML code below:

<p>Hello, bla-bla-bla</p>
<a href="somesite">Click here</a>

You'll see that it lacks html/body tags. Adding body and html tags to the document is what I want to achieve.

I attempted to add an HTML element using the code below:

 var el = doc.CreateElement("html");
 var nodes = doc.DocumentNode.ChildNodes;
 doc.DocumentNode.RemoveAllChildren();
 el.AppendChildren(nodes);    
 doc.DocumentNode.AppendChild(el);  

But then came a call.doc.DocumentNode.WriteContentTo() returns <html></html> If I alter the final lines' execution order:

var el = doc.CreateElement("html");
var nodes = doc.DocumentNode.ChildNodes;
doc.DocumentNode.RemoveAllChildren();
doc.DocumentNode.AppendChild(el); 
el.AppendChildren(nodes);  

I gotSystem.StackOverflowException once thedoc.DocumentNode.WriteContentTo() .

Possible resolutions include the following:

doc.LoadHtml("<html>" + doc.DocumentNode.WriteContentTo() + "</html>")

but the complete document reparsing will render it useless.

Do you have any suggestions for a performance-effective solution to this issue?

1
1
6/25/2012 9:15:37 AM

Accepted Answer

I finally made it function. The reason the first example didn't work wasdoc.DocumentNode.ChildNodes not the duplicate ofHtmlNodeCollection but the actual node collection. Before adding them to the collection, it resulted in the deletion of all nodes.el . The following code works as intended:

var el = doc.CreateElement("html");
var nodes = doc.DocumentNode.ChildNodes;
el.AppendChildren(nodes);    
doc.DocumentNode.RemoveAllChildren();
doc.DocumentNode.AppendChild(el);  
2
6/25/2012 1:47:55 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow