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

发布于 2024-08-17 15:23:00 字数 441 浏览 2 评论 0原文

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

escapeXml
unescapeXml
escapeHtml
unescapeHtml

方法)。有 .Net 等效项吗?或者也许是一些完全符合逻辑的 C# 代码可以完成同样的事情?

I'm porting over a Java project that I wrote which uses the Apache Commons Lang StringEscapeUtils class (particularly the

escapeXml
unescapeXml
escapeHtml
unescapeHtml

methods). Is there a .Net equivalent? Or perhaps some totally logical bit of C# code that accomplishes the same thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魂ガ小子 2024-08-24 15:23:00

Using System.Web for HTML there is:

For XML escaping you can use the SecurityElement.Escape method in the System.Security namespace. However, there's no equivalent to unescape it that I am aware of. You would have to write your own method that replaces the encoded XML, such as < and & etc. with their equivalents. The link for the escape method lists the other items.

寒冷纷飞旳雪 2024-08-24 15:23:00
public static class StringEscapeUtils
{
    public static string EscapeXml(string unescaped)
    {
        return SecurityElement.Escape(unescaped);
    }

    public static string UnescapeXml(string escaped)
    {
        return escaped.Replace("<", "<")
                      .Replace(">", ">")
                      .Replace(""", "\"")
                      .Replace("'", "'")
                      .Replace("&", "&");
    }

    public static string EscapeHtml(string unescaped)
    {
        return HttpUtility.HtmlEncode(unescaped);
    }

    public static string UnescapeHtml(string escaped)
    {
        return HttpUtility.HtmlDecode(escaped);
    }
}
public static class StringEscapeUtils
{
    public static string EscapeXml(string unescaped)
    {
        return SecurityElement.Escape(unescaped);
    }

    public static string UnescapeXml(string escaped)
    {
        return escaped.Replace("<", "<")
                      .Replace(">", ">")
                      .Replace(""", "\"")
                      .Replace("'", "'")
                      .Replace("&", "&");
    }

    public static string EscapeHtml(string unescaped)
    {
        return HttpUtility.HtmlEncode(unescaped);
    }

    public static string UnescapeHtml(string escaped)
    {
        return HttpUtility.HtmlDecode(escaped);
    }
}
慈悲佛祖 2024-08-24 15:23:00

您可以使用 HtmlEncodeHtmlDecode 用于 Html 替换。您可以找到 XML 转义选项 此处

You could use the HtmlEncode and HtmlDecode for the Html replacements. Your options for XML escaping can be found here.

月野兔 2024-08-24 15:23:00

查看 HttpServerUtility

HttpServerUtility.HtmlEncode、HtmlDecode、UrlDecode 等...

也适用于 XML:
System.Xml.XmlConvert
System.Security.SecurityElement。逃脱

Check out HttpServerUtility Class

HttpServerUtility.HtmlEncode, HtmlDecode, UrlDecode etc etc...

Also for XML:
System.Xml.XmlConvert
System.Security.SecurityElement.Escape

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文