I am using CsQuery in a C# Project. As a simple example I have a p-Tag which has a nested b-Tag in it:
<p>Lorem ipsum dolor sit amet, sanctus <b>TEST</b> Lorem ipsum dolor sit amet.</p>
I need to modify the plain-text in each Tag, meaning the text inside the p-Tag and inside the b-Tag, with another method (louis()):
IDomObject htmlelement = dom.Get(i);
if (!(htmlelement.FirstElementChild == null))
{
string test = htmlelement.InnerHTML;
//test: Lorem ipsum dolor sit amet, sanctus <b>TEST</b> Lorem ipsum dolor sit amet.
STDIN = htmlelement.FirstElementChild.InnerText;
OUTPUT = louis(command, param, STDIN);
htmlelement.FirstElementChild.InnerText = OUTPUT;
STDIN = htmlelement.InnerText;
OUTPUT = louis(command, param, STDIN);
htmlelement.InnerText = OUTPUT;
test = htmlelement.InnerHTML;
//test now loses the nested <b>TEST</b>
//test: Lorem ipsum dolor sit amet, sanctus Lorem ipsum dolor sit amet.
}
When I try to replace (only) the text inside the p-Tag with the new text, the nested b-Tag is lost. How can I prevent this and I'm sure there is a better way to manipulate nested Elements since it is a powerful framework. I just haven't figured it out yet.
There's a bug in the release version of CsQuery for InnerText
... see https://github.com/jamietre/CsQuery/issues/82
Either use the Text()
method, which is a CQ
method so you'd need to wrap the element in a CQ
object, e.g.
STDIN = htmlelement.FirstElementChild.Cq().Text();
..or update to the current prerelease version 1.3.5 beta 5 on nuget which fixes this problem: http://www.nuget.org/packages/CsQuery/1.3.5-beta5
Humblest apologies that this has remained so long in the release version which is a year old now!