Is there an implementation of org.apache.commons.lang.StringEscapeUtils for .Net?

I am porting a Java project that I wrote that uses the Apache Commons Lang StringEscapeUtils class (especially

escapeXml
unescapeXml
escapeHtml
unescapeHtml

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

+3
source share
4 answers

Using System.Web for HTML:

XML SecurityElement.Escape System.Security. unescape, . , XML, < & .. . escape .

+4
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

HttpServerUtility

HttpServerUtility.HtmlEncode, HtmlDecode, UrlDecode .. ..

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

+1
source

Source: https://habr.com/ru/post/1727833/


All Articles