Understanding HTML Encoding:
HTML encoding is the process of converting special characters into their corresponding HTML entities. This is essential when displaying user-generated content on a web page to prevent the interpretation of input as executable code. For instance, the less-than symbol <
is encoded as <
, and the greater-than symbol >
is encoded as >
.
using System;
using System.Web;
class Program
{
static void Main()
{
// Input string with special characters
string userInput = "<h1>Welcome to TechieHooK!</h1>";
// HTML encode the string
string encodedString = HttpUtility.HtmlEncode(userInput);
// Display the original and encoded strings
Console.WriteLine("Original String: " + userInput);
Console.WriteLine("Encoded String: " + encodedString);
Console.ReadKey();
}
}
We started with a sample string containing HTML tags. The HttpUtility.HtmlEncode
method is then applied to encode the string.
Output:
Original String: <h1>Welcome to TechieHooK!</h1>
Encoded String: <h1>Welcome to TechieHooK!</h1>
Comments (0)