代码之家  ›  专栏  ›  技术社区  ›  Jason Sperske

是否有用于.Net的org.apache.commons.lang.StringEscapeUtils的实现?

  •  3
  • Jason Sperske  · 技术社区  · 16 年前

    我正在移植我编写的一个Java项目,该项目使用 Apache Commons Lang StringEscapeUtils 类(尤其是

    escapeXml
    unescapeXml
    escapeHtml
    unescapeHtml
    

    方法)。是否有一个与.Net相当的版本?或者是一些完全符合逻辑的C代码来完成同样的事情?

    4 回复  |  直到 12 年前
        1
  •  4
  •   Ahmad Mageed    16 年前

    将System.Web用于HTML有:

    对于XML转义,可以使用 SecurityElement.Escape method < 和 & 等及其等价物。escape方法的链接列出了其他项。

        2
  •  3
  •   LukeH    16 年前
    public static class StringEscapeUtils
    {
        public static string EscapeXml(string unescaped)
        {
            return SecurityElement.Escape(unescaped);
        }
    
        public static string UnescapeXml(string escaped)
        {
            return escaped.Replace("&lt;", "<")
                          .Replace("&gt;", ">")
                          .Replace("&quot;", "\"")
                          .Replace("&apos;", "'")
                          .Replace("&amp;", "&");
        }
    
        public static string EscapeHtml(string unescaped)
        {
            return HttpUtility.HtmlEncode(unescaped);
        }
    
        public static string UnescapeHtml(string escaped)
        {
            return HttpUtility.HtmlDecode(escaped);
        }
    }
    
        3
  •  1
  •   Yuriy Faktorovich    16 年前

    HtmlEncode 和 HtmlDecode 用于Html替换。可以找到XML转义的选项 here

        4
  •  1
  •   o.k.w    16 年前

    退房 HttpServerUtility

    HttpServerUtility.HtmlEncode、HtmlDecode、UrlDecode等。。。

    同样对于XML:
    System.Xml.XmlConvert
    System.Security.SecurityElement.Escape