HTML Agility Pack allows you to replace a single node with several nodes.

.net c# html-agility-pack

Question

I am removing certain placeholder input tags and replacing them with HTML. Frequently, the HTML I use to replace them consists of only one tag, which is simple enough:

HtmlNode node = HtmlNode.CreateNode(sReplacementString);
inputNode.ParentNode.ReplaceChild(node, inputNode);

But if I wish to substituteinputNode at least two nodesHtmlNode.CreateNode(sReplacementString) reads just the first node. Is it possible to do a replace where?sReplacementString is several tags?

1
6
3/14/2012 7:10:24 PM

Accepted Answer

There isn't a direct method to accomplish it, as far as I know.HtmlNode.CreateNode if there are many nodes in the HTML snippet, just the first one is produced by the method.

You may make a temporary node, make its children nodes from the temporary node as a solution.sReplacementString , and then add these child nodes immediately below theinputNode node, then get rid of theinputNode .

var temp = doc.CreateElement("temp");
temp.InnerHtml = sReplacementString;
var current = inputNode;
foreach (var child in temp.ChildNodes)
{
    inputNode.ParentNode.InsertAfter(child, current);
    current = child;
}
inputNode.Remove();
8
3/14/2012 7:54:12 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