HttpUtility.HtmlEncode will not encode a single quote (') and is not suitable for encoding a string that will be used as a warning message. Personally, I do it like this:
public static string EscapeAlertMessage(string value) { value = value.Replace("\\", "\\\\"); value = value.Replace("'", "\\'"); value = value.Replace("\"", "\\\""); return value; }
If your message contains several lines, you can replace them with "\ n" - for example. if the lines are separated by the .NewLine environment:
value = value.Replace(Environment.NewLine, "\\n");
Or, if you do not know what the delimiters are (\ r \ n, \ n or \ r), you can use:
value = value.Replace("\r", "\\r"); value = value.Replace("\n", "\\n");
Joe
source share