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

c# html-agility-pack

Question

I am trying to get the inner text from a node but it has child nodes and its text is in the middle of its child entries i.e:

<script1>
 <p1>lalala</p1>
 "script text"
</script1>

The code I need is inside script1, but if I try and get innertext I get all of the inside of p1 too..

Cannot figure it out.

Accepted Answer

This code:

    HtmlDocument doc = new HtmlDocument();
    doc.Load(MyTextHtml);

    HtmlNode node = doc.DocumentNode.SelectSingleNode("//p1/following-sibling::text()");
    Console.WriteLine(node.InnerText.Trim());

will output this:

"script text"

Here is link on XPATH axes that should get you started.




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