如何将此字符串转换为普通字符串,以便将其放入HTML文档?

时间:2011-08-21 01:01:13

标签: c# html

如何转换此字符串

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

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

3 个答案:

答案 0 :(得分:4)

使用HttpUtility.HtmlDecode进行此类操作......

答案 1 :(得分:3)

MSDN article on HtmlDecode。您可以使用System.Web.HttpUtility.HtmlDecode()将这些转换为原始的HTML等效字符。

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.

答案 2 :(得分:0)

myEncodedString = HttpUtility.HtmlEncode(myString);

被盗: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());
      }
   }