I need to encode a whole text while leaving the < and > intact.
example
<p>Give me 100.000 €!</p>
must become:
<p>Give me 100.000 €!</p>
the html tags must remain intact
I need to encode a whole text while leaving the < and > intact.
example
<p>Give me 100.000 €!</p>
must become:
<p>Give me 100.000 €!</p>
the html tags must remain intact
Maybe use string.replace for just those characters you want to encode?
you might go for Html Agility Pack and then encode the values of the tags
You could use HtmlTextWriter in addition to htmlencode. So you would use HtmlTextWriter to setup your <p></p>
and then just set the body of the <p></p>
using HtmlEncode. HtmlTextWriter allow ToString(); and a bunch of other methods so it shouldn't be much more code.
Use a regular expression that matches either a tag or what's between tags, and encode what's between:
html = Regex.Replace(
html,
"(<[^>]+>|[^<]+)",
m => m.Value.StartsWith("<") ? m.Value : HttpUtility.HtmlEncode(m.Value)
);