I have a string with special characters like this:
äöüß&
Now I want to put that in an HTML document and need to escape these characters. Is there an elegant way doing this?
I could do this:
string html;
html = html.Replace("ü", "uu¨");
html = html.Replace("ß", "ß");
....
But I don't want to do that for all possible special characters.
Let the Framework do the work for you.
You could try HtmlEncode
:
string encodedHtml = Server.HtmlEncode(html);
There is also this, intended for XML, but it should work just fine with HTML:
System.Security.SecurityElement.Escape( string s );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With