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:
- Get first child node
- if the node is
#text, thannode.InnerHtml = InnerText.Replace(search_term, span_code) - if node has child node goto step 1
- goto next sibling then go to step 1
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.