http://weblogs.sqlteam.com/MLADENP/archive/2008/10/21/Different-ways-how-to-escape-an-XML-string-in-C.aspx
XML encoding is necessary if you have to save XML text in an XML document. If you don't escape special chars the XML to insert will become a part of the original XML DOM and not a value of a node.
Escaping the XML means basically replacing 5 chars with new values.
These replacements are:
< | -> | < |
> | -> | > |
" | -> | " |
' | -> | ' |
& | -> | & |
Here are 4 ways you can encode XML in C#:
1. string.Replace() 5 times
This is ugly but it works. Note that Replace("&", "&") has to be the first replace so we don't replace other already escaped &.
string xml = "<node>it's my \"node\" & i like it<node>";
encodedXml = xml.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("\"", """).Replace("'", "'");
// RESULT: <node>it's my "node" & i like it<node>
2. System.Web.HttpUtility.HtmlEncode()
Used for encoding HTML, but HTML is a form of XML so we can use that too. Mostly used in ASP.NET apps. Note that HtmlEncode does NOT encode apostrophes ( ' ).
string xml = "<node>it's my \"node\" & i like it<node>";
string encodedXml = HttpUtility.HtmlEncode(xml);
// RESULT: <node>it's my "node" & i like it<node>
3. System.Security.SecurityElement.Escape()
In Windows Forms or Console apps I use this method. If nothing else it saves me including the System.Web reference in my projects and it encodes all 5 chars.
string xml = "<node>it's my \"node\" & i like it<node>";
string encodedXml = System.Security.SecurityElement.Escape(xml);
// RESULT: <node>it's my "node" & i like it<node>
4. System.Xml.XmlTextWriter
Using XmlTextWriter you don't have to worry about escaping anything since it escapes the chars where needed. For example in the attributes it doesn't escape apostrophes, while in node values it doesn't escape apostrophes and qoutes.
string xml = "<node>it's my \"node\" & i like it<node>";
using (XmlTextWriter xtw = new XmlTextWriter(@"c:\xmlTest.xml", Encoding.Unicode))
{
xtw.WriteStartElement("xmlEncodeTest");
xtw.WriteAttributeString("testAttribute", xml);
xtw.WriteString(xml);
xtw.WriteEndElement();
}
// RESULT:
/*
<xmlEncodeTest testAttribute="<node>it's my "node" & i like it<node>">
<node>it's my "node" & i like it<node>
</xmlEncodeTest>
*/
Each of the four ways is different, so use each one where you fell appropriate. You can't go wrong with SecurityElement though. :)
Online HTML Encoder/Decoder:
http://foxtools.ru/HtmlEncoder
Some HTML decode test:
using System.Web;
using System.Xml.Linq;
using System.Net;
namespace console
{
class Program
{
static void Main(string[] args)
{
//
string special_xml = "Алиса в Стране чудес / Alice's Adventures in Wonderland";
// Алиса в Стране чудес / Alice&apos;s Adventures in Wonderland
string encodedXml_escape = System.Security.SecurityElement.Escape(special_xml);
// Optimal one
// Алиса в Стране чудес / Alice's Adventures in Wonderland
string encodedXml_http = HttpUtility.HtmlDecode(special_xml);
string encodedXml_web = WebUtility.HtmlDecode(special_xml);
//Необработанное исключение типа "System.Xml.XmlException" в System.Xml.dll
string encodedXml = XDocument.Parse(special_xml).ToString();
}
}
}