I can easily remove the element just by note.Remove() lik this:
HtmlDocument html = new HtmlDocument();
html.Load(Server.MapPath(@"~\Site\themes\default\index.cshtml"));
foreach (var item in html.DocumentNode.SelectNodes("//removeMe"))
{
item.Remove();
}
But that removes the innerHtml as well. What if i only want to remove the tag, and keep the innerHtml?
Example:
<ul>
<removeMe>
<li>
<a href="#">Keep me</a>
</li>
</removeMe>
</ul>
Any help would be appreciated :)
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var node = doc.DocumentNode.SelectSingleNode("//removeme");
node.ParentNode.RemoveChild(node, true);
This should work:
foreach (var item in doc.DocumentNode.SelectNodes("//removeMe"))
{
if (item.PreviousSibling == null)
{
//First element -> so add it at beginning of the parent's innerhtml
item.ParentNode.InnerHtml = item.InnerHtml + item.ParentNode.InnerHtml;
}
else
{
//There is an element before itemToRemove -> add the innerhtml after the previous item
foreach(HtmlNode node in item.ChildNodes){
item.PreviousSibling.ParentNode.InsertAfter(node, item.PreviousSibling);
}
}
item.Remove();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With