System.Web Namespace HttpUtility Class
Converts a string that has been HTML-encoded for HTTP transmission into a decoded string.
1. Converts a string that has been HTML-encoded for HTTP transmission into a decoded string.
2. Converts a string that has been HTML-encoded into a decoded string, and sends the decoded string to a TextWriter output stream.
The following example demonstrates the HtmlEncode and HtmlDecode methods of the HttpUtility class. The input string is taken from user and encoded using the HtmlEncode method.The encoded string thus obtained is then decoded using the HtmlDecode method.
NOTE: This example shows how to use one of the overloaded versions of HtmlDecode. For other examples that might be available, see the individual overload topics.
[ VB ]
Imports System
Imports System.Web
Imports System.IO
Class myNewClass
Public Shared Sub Main ( )
Dim myString As String
Response.Write ( "Enter a string having '&' or '""' in it: " )
myString = Console.ReadLine ( )
Dim myEncodedString As String
' Encode the string.
myEncodedString = HttpUtility.HtmlEncode ( myString )
Response.Write ( "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
[ C# ]
using System;
using System.Web;
using System.IO;
class myNewClass {
public static void Main ( ) {
String myString;
Response.Write ( "Enter a string having '&' or '\"' in it: " );
myString=Console.ReadLine ( );
String myEncodedString;
// Encode the string.
myEncodedString = HttpUtility.HtmlEncode ( myString );
Response.Write ( "HTML Encoded string is "+myEncodedString );
StringWriter myWriter = new StringWriter ( );
// Decode the encoded string.
HttpUtility.HtmlDecode ( myEncodedString, myWriter );
Console.Write ( "Decoded string of the above encoded string is " +
myWriter.ToString ( ) );
}
}
HttpUtility Members