Html Agility Pack - Remove element, but not innerHtml

后端 未结 10 2077
予麋鹿
予麋鹿 2021-01-06 11:46

I can easily remove the element just by note.Remove() lik this:

HtmlDocument html = new HtmlDocument();

html.Load(Server.MapPath(@\"~\\Site\\themes\\default         


        
10条回答
  •  既然无缘
    2021-01-06 12:12

    Perhaps this might be what you're looking for?

    foreach (HtmlNode node in html.DocumentNode.SelectNodes("//removeme"))
    {
        HtmlNodeCollection children = node.ChildNodes; //get 's children
        HtmlNode parent = node.ParentNode; //get 's parent
        node.Remove(); //remove 
        parent.AppendChildren(children); //append the children to the parent
    }
    

    Edit: L.B's answer is much cleaner. Go with his!

提交回复
热议问题