CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Html Encoding

  1. #1
    Join Date
    Jan 2005
    Posts
    224

    Html Encoding

    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
    Last edited by tis707; December 15th, 2008 at 03:47 AM. Reason: typo

  2. #2

    Re: Html Encoding

    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:

    Code:
    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());
          }
       }
    Last edited by ZOverLord; December 15th, 2008 at 05:37 AM.

  3. #3
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Html Encoding

    Quote Originally Posted by ZoverLord
    the characters < and > are encoded as &lt; and &gt; for HTTP transmission."
    very good example.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured