HttpUtility.HtmlEncode HttpUtility.HtmlEncode HttpUtility.HtmlEncode HttpUtility.HtmlEncode Method

Definition

Converts a string into an HTML-encoded string.

To encode or decode values outside of a web application, use the WebUtility class.

Overloads

HtmlEncode(Object) HtmlEncode(Object) HtmlEncode(Object) HtmlEncode(Object)

Converts an object's string representation into an HTML-encoded string, and returns the encoded string.

HtmlEncode(String) HtmlEncode(String) HtmlEncode(String) HtmlEncode(String)

Converts a string to an HTML-encoded string.

HtmlEncode(String, TextWriter) HtmlEncode(String, TextWriter) HtmlEncode(String, TextWriter) HtmlEncode(String, TextWriter)

Converts a string into an HTML-encoded string, and returns the output as a TextWriter stream of output.

HtmlEncode(Object) HtmlEncode(Object) HtmlEncode(Object) HtmlEncode(Object)

Converts an object's string representation into an HTML-encoded string, and returns the encoded string.

public:
 static System::String ^ HtmlEncode(System::Object ^ value);
public static string HtmlEncode (object value);
static member HtmlEncode : obj -> string
Public Shared Function HtmlEncode (value As Object) As String

Parameters

value
Object Object Object Object

An object.

Returns

An encoded string.

Remarks

To encode or decode values outside of a web application, use the WebUtility class.

HtmlEncode(String) HtmlEncode(String) HtmlEncode(String) HtmlEncode(String)

Converts a string to an HTML-encoded string.

public:
 static System::String ^ HtmlEncode(System::String ^ s);
public static string HtmlEncode (string s);
static member HtmlEncode : string -> string
Public Shared Function HtmlEncode (s As String) As String

Parameters

s
String String String String

The string to encode.

Returns

An encoded string.

Examples

The following code example demonstrates the HtmlEncode and HtmlDecode methods of the HttpUtility class. The input string is encoded using the HtmlEncode method. The encoded string obtained is then decoded using the HtmlDecode method.

using System;
using System.Web;
using System.IO;

class MyNewClass
{
    public static void Main()
    {
        Console.WriteLine("Enter a string having '&', '<', '>' or '\"' in it: ");
        string myString = Console.ReadLine();

        // Encode the string.
        string myEncodedString = HttpUtility.HtmlEncode(myString);

        Console.WriteLine($"HTML Encoded string is: {myEncodedString}");
        StringWriter myWriter = new StringWriter();

        // Decode the encoded string.
        HttpUtility.HtmlDecode(myEncodedString, myWriter);

        string myDecodedString = myWriter.ToString();
        Console.Write($"Decoded string of the above encoded string is: {myDecodedString}");
    }
}
Imports System
Imports System.Web
Imports System.IO

Class MyNewClass
   Public Shared Sub Main()
      Dim myString As String
      Console.WriteLine("Enter a string having '&' or '""'  in it: ")
      myString = Console.ReadLine()
      Dim myEncodedString As String
      ' Encode the string.
      myEncodedString = HttpUtility.HtmlEncode(myString)
      Console.WriteLine("HTML Encoded string is " + myEncodedString)
      Dim myWriter As New StringWriter()
      ' Decode the encoded string.
      HttpUtility.HtmlDecode(myEncodedString, myWriter)
      Console.Write("Decoded string of the above encoded string is " + myWriter.ToString())
   End Sub 'Main
End Class 'MyNewClass

Remarks

If characters such as blanks and punctuation are passed in an HTTP stream, they might be misinterpreted at the receiving end. HTML encoding converts characters that are not allowed in HTML into character-entity equivalents; HTML decoding reverses the encoding. For example, when embedded in a block of text, the characters < and > are encoded as &lt; and &gt; for HTTP transmission.

To encode or decode values outside of a web application, use the WebUtility class.

See also

HtmlEncode(String, TextWriter) HtmlEncode(String, TextWriter) HtmlEncode(String, TextWriter) HtmlEncode(String, TextWriter)

Converts a string into an HTML-encoded string, and returns the output as a TextWriter stream of output.

public:
 static void HtmlEncode(System::String ^ s, System::IO::TextWriter ^ output);
public static void HtmlEncode (string s, System.IO.TextWriter output);
static member HtmlEncode : string * System.IO.TextWriter -> unit
Public Shared Sub HtmlEncode (s As String, output As TextWriter)

Parameters

s
String String String String

The string to encode

Remarks

If characters such as blanks and punctuation are passed in an HTTP stream, they might be misinterpreted at the receiving end. HTML encoding converts characters that are not allowed in HTML into character-entity equivalents; HTML decoding reverses the encoding. For example, when embedded in a block of text, the characters < and >, are encoded as &lt; and &gt; for HTTP transmission.

To encode or decode values outside of a web application, use the WebUtility class.

See also

Applies to