Click to See Complete Forum and Search --> : Html Encoding


tis707
December 15th, 2008, 02:28 AM
Hi all

I am getting problem in encoding the text into HTMl in windows forms

I have added reference System.web

string test = "< & >";
string test1 = HttpUtility.HtmlEncode(test)



but i am unable to do the encoding just i am getting the same string

Thanks a lot

ZOverLord
December 15th, 2008, 04:29 AM
This is the way it should be.

"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."

from: http://msdn.microsoft.com/en-us/library/73z22y6h.aspx

Your string is NOT embedded in a block of text, your AND sign is between a left and a right arrow <> and not a right and left arrow >< or not between any arrows at all ;-)

Try the example shown:


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

class MyNewClass
{
public static void Main()
{
String myString;
Console.WriteLine("Enter a string having '&' or '\"' in it: ");
myString=Console.ReadLine();
String myEncodedString;
// Encode the string.
myEncodedString = HttpUtility.HtmlEncode(myString);
Console.WriteLine("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());
}
}

toraj58
December 15th, 2008, 09:03 AM
the characters < and > are encoded as &lt; and &gt; for HTTP transmission."


very good example.