4

Is there any 'slimmer' alternative to the System.Web.HttpUtility.HtmlEncode/.Decode functions in .net 3.5 (sp1)? A separate library is fine.. or even 'wanted', at least something that does not pull in a 'whole new world' of dependencies that System.Web requires.

I only want to convert a normal string into its xml/xhtml compliant equivalent (& back).

4

6 回答 6

26

在 .NET Framework 4.0 中,System.Net.WebUtility.HtmlEncode也许?请注意,此类位于 System.dll 而不是 System.Web.dll中。

于 2010-05-06T09:34:19.413 回答
5

对于 XML,您只需对具有特殊含义的字符进行编码,因此您可以使用以下简单的方法:

public static string XmlEncode(string value) {
  return value
    .Replace("<", "&lt;")
    .Replace(">", "&gt;")
    .Replace("\"", "&quot;")
    .Replace("'", "&apos;")
    .Replace("&", "&amp;");
}

public static string XmlDecode(string value) {
  return value
    .Replace("&lt;", "<")
    .Replace("&gt;", ">")
    .Replace("&quot;", "\"")
    .Replace("&apos;", "'")
    .Replace("&amp;", "&");
}
于 2010-05-06T08:34:17.423 回答
4

对于 HTML,如果使用 .NET Framework 4.0 的System.Net.WebUtility不是一个可行的解决方案,您可以使用这个:

string HtmlEncode(string s)
{
    if (s == null)
    {
        return null;
    }

    var result = new StringBuilder(s.Length);

    foreach (char ch in s)
    {
        if (ch <= '>')
        {
            switch (ch)
            {
                case '<':
                    result.Append("&lt;");
                    break;

                case '>':
                    result.Append("&gt;");
                    break;

                case '"':
                    result.Append("&quot;");
                    break;

                case '\'':
                    result.Append("&#39;");
                    break;

                case '&':
                    result.Append("&amp;");
                    break;

                default:
                    result.Append(ch);
                    break;
            }
        }
        else if (ch >= 160 && ch < 256)
        {
            result.Append("&#").Append(((int)ch).ToString(CultureInfo.InvariantCulture)).Append(';');
        }
        else
        {
            result.Append(ch);
        }
    }

    return result.ToString();
}

执行的原因:

对字符串执行大量 Replace() 将非常无效,尤其是在大字符串上。

免责声明:

此解决方案的灵感来自在 .NET Framework 4.0 系统程序集上使用JetBrains dotPeek 。

于 2013-09-18T22:40:36.100 回答
1

如果可能的话,您可以从 Mono 代码中“借用”HttpUtility 类并将其直接编译为您的实用程序程序集。

于 2010-05-06T08:35:56.230 回答
1

尽管编码可能看起来很简单,但我强烈建议使用广泛使用的库,以最大程度地降低安全漏洞的风险。Microsoft 的反跨站点脚本库提供了用于 Html/Xml/Javascript 转义和相应属性转义的方法,应该可以满足您的大部分 Web 需求。

于 2010-05-06T08:43:14.080 回答
0

与@marco 相同,但语言是 powershell

function Invoke-HTMLEncode
{ #https://stackoverflow.com/questions/2779594/alternative-to-system-web-httputility-htmlencode-decode

  param($string)
  if([string]::isNullorEmpty($string))
  { 
   $return = $null
  }
  $result = [system.text.stringbuilder]::new($string.length)
  foreach($ch in $string.ToCharArray()) 
  {
    if([byte][char]$ch -le [byte][char]'>')
    {
     switch ($ch)
     {
       '<' {
         $result.append("&lt;") | out-null
         break;
       }
       '>' {
        $result.append("&gt;")| out-null
        break;
      }
      '"' {
        $result.append("&quot;")| out-null
        break;
      }
      '&'{
        $result.append("&amp;")| out-null
        break;
      }
      default {
        $result.append($ch)| out-null
        break;
      }
     } 
    }
    elseif([byte][char]$ch -ge 160 -and [byte][char]$ch -lt 256)
    {
      #result.Append("&#").Append(((int)ch).ToString(CultureInfo.InvariantCulture)).Append(';');
      $result.append("&#").append(([byte][char]$ch).toString([System.Globalization.CultureInfo]::InvariantCulture)).append(';') | out-null
    }
    else
    {
      $result.Append($ch) | out-null
    }
  }
  $result.ToString()
}
于 2018-05-25T03:04:42.587 回答