Escape
// Approach 1string xml = "<custom>some custom Xml data</custom>";
string xmlEscaped= System.Security.SecurityElement.Escape(xml);
// xmlEscaped== <custom>some custom Xml data</custom>
// xmlEscaped can be set as a node value in another Xml doc
// Approach 2
string customXml= "<custom>some custom Xml data</custom>";
using (XmlTextWriter containerXml= new XmlTextWriter(@"c:\containerDoc.xml", Encoding.Unicode))
{
containerXml.WriteStartElement("Container");
// WriteString automatically encodes the customXml
containerXml.WriteString(customXml);
containerXml.WriteEndElement();
}
Output
<Container>
<custom>some custom Xml data</custom>
</Container>
Un-Escape
To read encoded Xml, grab the escaped Xml text from the Container document.
// escapedXml is obtained from a node in the Container document
string escapedXml = "<custom>some custom Xml data</custom>";
string xml = HttpUtility.HtmlDecode(escapedXml);
var doc = new XmlDocument();
doc.LoadXml(xml);
Output
<custom>some custom Xml data</custom>