Two methods to escape and unescape XML strings.
/// <summary>
/// XML escape a string.
/// </summary>
/// <param name="unescaped">The string to escape.</param>
/// <returns>An XML escaped string.</returns>
/// <remarks>https://stackoverflow.com/questions/1132494/string-escape-into-xml</remarks>
public static string XmlEscape(string unescaped)
{
var doc = new XmlDocument();
XmlNode node = doc.CreateElement("root");
node.InnerText = unescaped;
return node.InnerXml;
}
/// <summary>
/// Unescape a previously XML escaped string.
/// </summary>
/// <param name="escaped">The escaped XML string.</param>
/// <returns>An unescaped XML string.</returns>
/// <remarks>https://stackoverflow.com/questions/1132494/string-escape-into-xml</remarks>
public static string XmlUnescape(string escaped)
{
var doc = new XmlDocument();
XmlNode node = doc.CreateElement("root");
node.InnerXml = escaped;
return node.InnerText;
}