How to encode special characters in XML
You can use a native .NET method for escaping special characters in text. Sure, there's only like 5 special characters, and 5 Replace() calls would probably do the trick, but I'm sure there's got to be something built-in.
Example of converting "&"
to "&"
To much relief, I've discovered a native method, hidden away in the bowels of the SecurityElement class. Yes, that's right - SecurityElement.Escape(string s) will escape your string and make it XML safe.
This is important, since if we are copying or writing data to Infopath Text fields, it needs to be first Escaped to non-Entity character like "&"
.
invalid XML Character to Replaced With
"<" to "<"
">" to ">"
"\"" to """
"'" to "'"
"&" to "&"
Namespace is "System.Security". Refer : http://msdn2.microsoft.com/en-us/library/system.security.securityelement.escape(VS.80).aspx
The Other Option is to Customise code for
public static string EscapeXml( this string s )
{
string toxml = s;
if ( !string.IsNullOrEmpty( toxml ) )
{
// replace literal values with entities
toxml = toxml.Replace( "&", "&" );
toxml = toxml.Replace( "'", "'" );
toxml = toxml.Replace( "\"", """ );
toxml = toxml.Replace( ">", ">" );
toxml = toxml.Replace( "<", "<" );
}
return toxml;
}
public static string UnescapeXml( this string s )
{
string unxml = s;
if ( !string.IsNullOrEmpty( unxml ) )
{
// replace entities with literal values
unxml = unxml.Replace( "'", "'" );
unxml = unxml.Replace( """, "\"" );
unxml = unxml.Replace( ">", ">" );
unxml = unxml.Replace( "<", "<" );
unxml = unxml.Replace( "&", "&" );
}
return unxml;
}
You can use HttpUtility.HtmlDecode or with .NET 4.0+ you can also use WebUtility.HtmlDecode
There are 3 other ways this can be done from what you tried:
- Use string.Replace() 5 times
- Use System.Web.HttpUtility.HtmlEncode()
- System.Xml.XmlTextWriter
I could explain each case but I found this link to be mightily useful.
Instead of System.Net.WebUtility.HtmlEncode
you have to use System.Net.WebUtility.HtmlDecode