I know this is old, but after some searching, I could not find anything better, so here is what I use:
public static string TextToHtml(string text) { text = HttpUtility.HtmlEncode(text); text = text.Replace("\r\n", "\r"); text = text.Replace("\n", "\r"); text = text.Replace("\r", "<br>\r\n"); text = text.Replace(" ", " "); return text; }
If for some reason you cannot use HttpUtility, you will have to do HTML encoding in a different way, and there are a lot of small details to worry about (and not just <>& ).
HtmlEncode only processes special characters, so after that I convert any combination of carriage return and / or line to BR tag and any double spaces to single space plus NBSP.
If desired, you can use the PRE tag for the last part, for example:
public static string TextToHtml(string text) { text = "<pre>" + HttpUtility.HtmlEncode(text) + "</pre>"; return text; }
eselk
source share