HtmlEncode, HtmlDecode.` HTML must sometimes be encoded. This is necessary for it to be displayed as text in another HTML document. With the WebUtility.HtmlEncode and WebUtility.HtmlDecode methods in the C# language, we do this without writing any custom code. `Example.` The HtmlEncode method is designed to receive a string that contains HTML markup characters such as > and <. The HtmlDecode method, meanwhile, is designed to reverse those changes. It changes encoded characters back to actual HTML. `Next: `We use HtmlEncode and HtmlDecode in a C# program. The System.Net assembly is included at the top of the program.`Example 2.` Next, HtmlEncode and HtmlDecode are also built into the Server objects in ASP.NET. These methods have no advantages over the HttpUtility methods. They are equivalent. We present an example that uses them in a Page class. `In this example,` we see three different methods. The first two just return an encoded or decoded string, and the HtmlTextWriter uses an interesting method called WriteEncodedText. `This may be more efficient, as it could avoid a string copy. I tested these methods with breakpoints.`HtmlTextWriter `htmltextwriter`The WebUtility class` is a better way to encode HTML and URLs in programs. You will want to call WebUtility.HtmlDecode and WebUtility.HtmlEncode on your strings. It is also possible to use the HttpUtility class.`Performance.` In my brief benchmarks, I found Server.HtmlEncode and Server.HtmlDecode to be much faster than my home-grown version that used StringBuilder. Unless you want create a better implementation, it is best to use these Framework methods. `StringBuilder `stringbuilder`Summary.` These methods provide reliable replacement of HTML characters and are available in all your .NET programs. HtmlEncode and HtmlDecode also handle character entities. These are sequences that represent non-ASCII characters. `These methods are also available on the HttpUtility type. More information on the HttpUtility type is available.`HttpUtility `httputility

FGKqQNot HTML encodedQKKYou & me > themKKQIs HTML encodedQKKYou &amp; me &gt; themQHtmlEncodeGVHtmlDecode mGgs: C#QKKGDGQ;KGDGQ.NetGbGJGAK{KFG%F{KFFGP aGyWebUtility.QHtmlEncodeQ(q<html><head><title>T</title></head></html>q);KFFGP bGyWebUtility.QHtmlDecodeQ(a)GbFFG'qAfter HtmlEncode: qG}a);KFFG'qAfter HtmlDecode: qG}b);KF}K}KKQKKAfter HtmlEncode:K&lt;html&gt;&lt;head&gt;&lt;title&gt;T&lt;/title&gt;&lt;/head&gt;&lt;/html&gtGbAfter HtmlDecode:K<html><head><title>T</title></head></html>QASPX code-behind file that encodes HTML: C#QKKGDGQ;KGDGQ.IO;KGDGQ.Web;KGDGQ.Web.UIGbGBpartial GJ_Default : PageK{KFprotected GcPage_Load(object sender, EventArgs e)KF{QKFFG{This could mess up HTML.KFFQGP textGyqyou & me > themq;Q G{1KKFFG{G1 > with >KFFQGP htmlEncodedGyServer.HtmlEncode(text);Q G{2KKFFG{Now has the > again.KFFQGP originalGyServer.HtmlDecode(htmlEncoded);Q G{3KKFFG{This is how you can access the ServerGpany class.KFFQGP alsoEncodedGyHttpContext.Current.Server.HtmlEncode(text);Q G{4KKFFQGOGar GPGarGyGqGOGar();KFFGD(HtmlTextGar writerGyGqHtmlTextGar(GPGar))KFF{QKFFFG{Ga a DIV with encoded text.KFFFQwriter.RenderBeginTag(HtmlTextGarTag.Div);KFFFwriter.GaEncodedText(text);KFFFwriter.RenderEndTag();KFF}KFFGP htmlGyGPGar.ToGO();Q G{5KFQ}K}KKQNotesQKKStep 1: QBeG|e encoding has occurred.QKGO: you & me > themKKStep 2: QThe GP is encoded GwHTML.QKGO: you &amp; me &gt; themKKStep 3: QGO is cGNed back from HTML.QKGO: you & me > themKKStep 4: QThe GP is encoded GwHTML again.QKGO: you &amp; me &gt; themKKStep 5: QThe HTML GP is written Gzo a DIV.QKText: <div>you &amp; me &gt; them</div>Q