Escape text for HTML

user34537 picture user34537 · Jun 17, 2009 · Viewed 124.5k times · Source

How do i escape text for html use in C#? I want to do

sample="<span>blah<span>"

and have

<span>blah<span>

show up as plain text instead of blah only with the tags part of the html :(. Using C# not ASP

Answer

Michael S. Scherotter picture Michael S. Scherotter · Jun 17, 2009
using System.Web;

var encoded = HttpUtility.HtmlEncode(unencoded);