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

本文关键字:字符串 HTML 文档 转换 | 更新日期: 2023-09-27 18:06:26

如何转换此字符串

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

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

使用HttpUtility.HtmlDecode来处理这些事情。。。

请参阅这篇关于HtmlDecode的MSDN文章。您可以使用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.
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());
      }
   }