I am trying to highlight text in an html line using the Html Agility Pack . I can replace the text with <span class="highlight">
, but when I replace the text, the empty space around the span tag will disappear. For example, if the text "This text will be highlighted"
, it appears as "This text will be<span class='highlighted'>highlighted</span>"
, and a space before the span tag. This combines the words before and after the span with the range text. I just do a recursive loop:
- Get the first child node
- if node
#text
than node.InnerHtml = InnerText.Replace(search_term, span_code)
- if node has a node child go to step 1
- go to the next brother, go to step 1
Then I get InnerHtml
of HtmlDocument
. I tried putting a space before <span
and after </span>
, but he deleted them. I tried HtmlDocument.OptionWriteEmptyNodes = true;
He didn't work either. I replaced all the characters "\n"
and "\t"
space before creating the HtmlDocument
and after receiving the html string, and this did not affect any of them.
How to save a space when using the Html Agility Pack?
source share