views:

226

answers:

4

I'm porting over a Java project that I wrote which uses the Apache Commons 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?

+2  A: 

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.

Ahmad Mageed
I'm voting this up as half the answer (just need the XML part) and for responding damn fast!
Jason Sperske
See update... adding links now...
Ahmad Mageed
I'm going to go ahead and say you nailed this one. XML unescaping is actually the most important thing for the task I'm currently, but knowing that there isn't a place to look in the system namespace is at least better than reading MSDN articles all day.
Jason Sperske
Oh yes, Mageed's response is damn fast, beats me by miles.
o.k.w
@Jason: Luke's response shows an example method so that saves you rolling your own. Also notice that others have suggested HttpServerUtility instead of HttpUtility. The encoding/decoding is similar, however the difference is that HttpServerUtility is in the context of a website and exposes the functionality via the `Server` object and uses HttpUtility internally. OTOH outside of that context you can use HttpUtility directly. So it boils down to what you have available depending on your project type.
Ahmad Mageed
@Ahmad: The `HtmlEncode`/`HtmlDecode` methods on `HttpServerUtility` are static and can be called directly, not just via the `Server` object exposed in ASP.NET. However, since those methods just pass straight through to the equivalent `HttpUtility` methods it makes more sense to just use `HttpUtility` directly.
LukeH
@Luke: thanks for clarifying!
Ahmad Mageed
A: 

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

Yuriy Faktorovich
A: 

Check out HttpServerUtility Class

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

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

o.k.w
+2  A: 
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);
    }
}
LukeH
I like the effort to wrap these various functions into a StringEscapeUtils class. These responses are neck and neck in terms of usefulness. I'm voting them both up, but I had to pick one as the "Answer" so I went with the first. You sir have certainly earned yor 17.1K reputation.
Jason Sperske