Point Deep

Mundeep's Tech Blog

How to Html Encode a string in a c# windows application?

Posted by mundeep on August 21, 2008

To HtmlEncode a string from within a Windows application you need to make sure that:

1) You have a reference to System.Web

2) You use the System.Web.HttpUtility.HtmlEncode method.

For example:

using System.Web;

public string MyHtmlEncode(string inputString) {
     return HttpUtility.HtmlEncode(inputString);

5 Responses to “How to Html Encode a string in a c# windows application?”

  1. Why does this not work in a VB.NET Windows Forms project?

    Imports System.Web

    Private Function EncodeText(ByVal sText As String) As String
    Return HttpUtility.HtmlEncode(sText)
    End Function

    The return value remains unchanged from the input (sText) variable.

  2. CSR said

    Does’nt work!

  3. kasam said

    yes it deosn’t work
    but an altenative solution is to use WebUtility.HtmlEncode

  4. Shefi said

    using System.Net

  5. Tiburon said

    you need add the reference (project -> add reference -> System.web) and insert “Imports System.Web.HttpUtility” so.. you can use directly HtmlEncode

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: