HtmlAgilityPack - Remove child nodes but retain inner text for the main node without XPath

c# html-agility-pack

Question

I have html like:

<div class="asd"> lalala </br> lalala2 <div> aaaaa </div> </div>

I need to get only "lalala \n lalala2" without using XPath.

Accepted Answer

You can create extension method for HtmlNode

public static class HtmlHelper
{
    public static string InnerText(this HtmlNode node)
    {
        var sb = new StringBuilder();
        foreach (var x in node.ChildNodes)
        {
            if (x.NodeType == HtmlNodeType.Text)
                sb.Append(x.InnerText);

            if (x.NodeType == HtmlNodeType.Element && x.Name == "br")
                sb.AppendLine();
        }

        return sb.ToString();
    }
}

just find the node and call node.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