how to remove div content from html and place the same on top of all divs using html agility pack

asp.net c# html-agility-pack

Question

i have a problem with html agility pack i am unable to remove div content from html and place the same content on top of all divs. like

<body>
   <div class="1">...</div>
   <div class="2">...</div>
   <div class="3">...</div>
</body>

now i want to remove/sort third div and place it on top of first div. Any help would be great. Thanks!

Popular Answer

You should try this code:

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml("<html><body><div class=\"1\">...</div><div class=\"2\">...</div><div class=\"3\">...</div></body></html>");
HtmlNode body = doc.DocumentNode.SelectSingleNode("/html/body");
HtmlNode div = body.SelectSingleNode("div[@class='3']");
if (div != null) {
    div.Remove();
    body.InsertBefore(div, body.FirstChild);
}



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why