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)

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

HtmlEncode(String)

Converts a string to an HTML-encoded string.

HtmlEncode(String, TextWriter)

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

HtmlEncode(Object)

Source:
HttpUtility.cs
Source:
HttpUtility.cs
Source:
HttpUtility.cs

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);
public static string HtmlEncode (object value);
static member HtmlEncode : obj -> string
Public Shared Function HtmlEncode (value As Object) As String

Parameters

value
Object

An object.

Returns

An encoded string.

Remarks

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

Applies to

HtmlEncode(String)

Source:
HttpUtility.cs
Source:
HttpUtility.cs
Source:
HttpUtility.cs

Converts a string to an HTML-encoded string.

public:
 static System::String ^ HtmlEncode(System::String ^ s);
public static string? HtmlEncode (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

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.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
End Class

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

HtmlEncode(String, TextWriter)

Source:
HttpUtility.cs
Source:
HttpUtility.cs
Source:
HttpUtility.cs

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);
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

The string to encode.

output
TextWriter

A TextWriter output stream.

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