Using C# - WinForms
I have a valid HTML string which may or may not contain various HTML elements such as <a>
.
I need to search this HTML and highlight certain keywords - the highlighting is done by adding a <span>
around the text with inline styling. I should not be doing this for <a>
tags, or any other HTML tag that isn't actually visible to the user.
e.g. currently I am doing this:
html = html.Replace(phraseToCount, "<span style=\"background: #FF0000; color: #FFFFFF; font-weight: bold;\">" + phraseToCount + "</span>");
This kind of works but it breaks <a>
tags. So in the example below only the 1st instance of the word cereal should end up with a <span>
around it:
<p>To view more types of cereal click <a href="http://www.cereal.com">here</a>.</p>
How could I do this?
EDIT - more info.
This will be running in a Winforms app as the best way to get the HTML is using the WebBrowser control - I will be scraping web pages and highlighting various words.