Preserve white space with HtmlAgilityPack

c# html-agility-pack html-parsing

Question

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:

  1. Get first child node
  2. if the node is #text, than node.InnerHtml = InnerText.Replace(search_term, span_code)
  3. if node has child node goto step 1
  4. 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?

Accepted Answer

Actually HtmlDocument.OptionWriteEmptyNodes = true; did what I want. I realized now.




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