I'm trying to highlight a text in the html string by using Html Agility Pack. I'm able to replace text with <span class="highlight">, but when I replace the text, the white space around the span tag is disappeared. For example, if the text is "This text will be highlighted", it results as "This text will be<span class='highlighted'>highlighted</span>", and white space gone before the span tag. This merges the words before and after the span with the span text. I simply do a recursive loop like that:
#text, than node.InnerHtml = InnerText.Replace(search_term, span_code)
Then I get the InnerHtml of the HtmlDocument as a result. I tried put space before <span and after </span>, but it removed them. I tried HtmlDocument.OptionWriteEmptyNodes = true; it didn't work either. I replaced all "\n" and "\t" chars with a space before creating HtmlDocument and after getting the html string, and it didn't affect neither.
How can I preserve the white space when using Html Agility Pack?
Actually HtmlDocument.OptionWriteEmptyNodes = true; did what I want. I realized now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With