It might be a little bit ugly, but you could just edit the InnerHtml attribute of the oldElement.ParentNode node like so:
if (oldElement.Name == "table")
{
oldElement.ParentNode.InnerHtml = "
<div class="overflow">
"
+ oldElement.OuterHtml +
"
</div>
";
}
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.ParentNode
becomes the <div>
node after AppendChild()
is called. The solution I found is to make another HtmlNode
at the start of the if block to hold the parent, and then calling ReplaceChild()
on that node at the end:
if (oldElement.Name == "table")
{
HtmlNode theParent = oldElement.ParentNode;
HtmlDocument doc = new HtmlDocument();
HtmlNode newElement = doc.CreateElement("div");
newElement.SetAttributeValue("class", "overflow");
newElement.AppendChild(oldElement);
theParent.ReplaceChild(newElement, oldElement);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…