How would you go about surrounding all tables with a <div class="overflow"></div> node? This apparently does not do it:
if (oldElement.Name == "table")
{
HtmlDocument doc = new HtmlDocument();
HtmlNode newElement = doc.CreateElement("div");
newElement.SetAttributeValue("class", "overflow");
newElement.AppendChild(oldElement);
oldElement.ParentNode.ReplaceChild(newElement, oldElement);
}
Nothing happens to the tables when I try that code. But if i use:
if (oldElement.Name == "table")
{
oldElement.Remove();
}
All tables are indeed removed, so I’m sure that i’m accessing the correct nodes.
It might be a little bit ugly, but you could just edit the InnerHtml attribute of the oldElement.ParentNode node like so:
It also doesn’t seem like you could edit the OuterHtml attribute of oldElement (which is why you have to get the ParentNode first). The HtmlAgilityPack says you can get/set OuterHtml, but VS2010 was telling me it’s a read-only property.
Edit
I was playing around with some code to figure this out and saw that
oldElement.ParentNodebecomes the<div>node afterAppendChild()is called. The solution I found is to make anotherHtmlNodeat the start of the if block to hold the parent, and then callingReplaceChild()on that node at the end: