How to remove all children nodes of selected node - html-agility-pack

c# html-agility-pack wpf

Question

Alright i want to remove all children nodes of this particular node

Here the node source code

<div class="Price fs30 clr8">
   7,
   <span class="PriceCurrency">73 TL
   <span class="kdv">KDV Dahil</span>
   </span>
   <div class="SaleDiv">
      %15
      <span>Ä°ndirim</span>
   </div>
</div>

So i want to remove all span children and div children - actually all children whatever is under the node

After removing these children i should get 7, as a innertext of the selected node

Ty very much for answers

c# .net 4.5 wpf

Accepted Answer

If you meant to keep only text nodes within the outer <div>, you can select all html child nodes using star XPath selector (*) and remove them. Here is an example in console application :

var html = @"<div class=""Price fs30 clr8"">
   7,
   <span class=""PriceCurrency"">73 TL
   <span class=""kdv"">KDV Dahil</span>
   </span>
   <div class=""SaleDiv"">
      %15
      <span>Ä°ndirim</span>
   </div>
</div>";
var doc = new HtmlDocument();
doc.LoadHtml(html);
var div = doc.DocumentNode.SelectSingleNode("//div[@class='Price fs30 clr8']");
foreach (HtmlNode node in div.SelectNodes("*"))
{
    node.Remove();
}
var innerText = div.InnerText.Trim();
Console.WriteLine(innerText);



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