1

How do I convert this string

<img alt="" src="http://win-thgn9fd7gfo:37996/Style Library/UWW/images/logo01.gif" style="BORDER: 0px solid; ">

to

<img alt="" src="http://win-thgn9fd7gfo:37996/Style Library/UWW/images/logo01.gif" style="BORDER: 0px solid;">
Darko
  • 38,310
  • 15
  • 80
  • 107
bhavinp
  • 823
  • 1
  • 9
  • 18
  • 1
    Try: http://stackoverflow.com/questions/122641/how-can-i-decode-html-characters-in-c – Josh Aug 21 '11 at 01:04

3 Answers3

4

Use HttpUtility.HtmlDecode for such things...

Yahia
  • 69,653
  • 9
  • 115
  • 144
3

See this MSDN article on HtmlDecode. You can use System.Web.HttpUtility.HtmlDecode() to convert those back to their original HTML equivalent characters.

var html = "<img alt=\"\" src=\"http://win-thgn9fd7gfo:37996/Style Library/UWW/images/logo01.gif\" style=\"BORDER: 0px solid; \">";

html = HttpUtility.HtmlEncode(html);
// html now = "&lt;img alt=&quot;&quot; src=&quot;http://win-thgn9fd7gfo:37996/Style Library/UWW/images/logo01.gif&quot; style=&quot;BORDER: 0px solid; &quot;&gt;"

var html = HttpUtility.HtmlDecode(html);
// html is now back to its original value.
Devin Burke
  • 13,642
  • 12
  • 55
  • 82
0
myEncodedString = HttpUtility.HtmlEncode(myString);

Stolen from : http://msdn.microsoft.com/en-us/library/aa332854%28v=vs.71%29.aspx

[C#] 
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());
      }
   }
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • Er, what's the answer here? Also, there's no need to post another answer and delete your old one; you can edit answers after they've been posted. – Adam Robinson Aug 21 '11 at 01:09
  • I didn't notice that the code was c# and the answer was for php, that's why I've delete it. – Pedro Lobito Aug 21 '11 at 01:14
  • But you could have edited your other answer to have this content (though this, admittedly, also does not answer the question without having to dig through it) rather than creating a new answer. – Adam Robinson Aug 21 '11 at 01:15
  • If you want straight answers go to www.getacoder.com. Users have to dig a little otherwise they'll never learn, if you want to post the correct answer go ahead. – Pedro Lobito Aug 21 '11 at 01:18
  • Your answer is unnecessarily obtuse. Have a look at the other answers to this question, all of which are correct and are not cluttered with extraneous information. The first part of your answer does not even make sense (the part before "stolen from..."), as it does not mention the class in question (`HttpUtility`). – Adam Robinson Aug 21 '11 at 01:20
  • The first part was posted by error, thanks for pointing that. – Pedro Lobito Aug 21 '11 at 01:23