I'm trying to write some text to a website using the System.Web.HttpUtility.HtmlEncode function in C#. The string parameter that I give to the f开发者_开发问答unction has some Environment.Newline's in it, but they are not written out on the website. Does anyone know why this is and how I can fix it. Thanks in advance.
Newline is written out as a physical line break so you will have to either wrap in a pre:
response.Write("<pre>" + HttpUtility.HtmlEncode(str) + "</pre>");
Or replace the new line with a BR AFTER you have HtmlEncoded (or it'll encode the BR as well):
response.Write(HttpUtility.HtmlEncode(str).Replace("\n", "<br />"));
Given its HTML NewLine characters do not display as whitespace. Try replacing your new line characters with <br/>
elements.
精彩评论