开发者

Write a new line to a web site using System.Web.HttpUtility.HtmlEncode in C#

开发者 https://www.devze.com 2023-03-26 02:57 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消