Special characters in XML?

By [)ia6l0 iii

Escape special characters in XML

Special characters include &, <, space etc. You need to encode and decode these characters accordingly to the XML representation.

This is where XMLConvert class comes handy. XMLConvert class has the EncodeName and the DecodeName methods for this very purpose. They convert these special characters into appropriate XML equivalents.

for e.g. If you need to create an element with value "My Post", the following line would throw error when the xml is viewed.
xmlTextWriter.WriteElementString("My Post", "600");

So, if we rewrite it like this, then the errors would disappear.
xmlTextWriter.WriteElementString(XmlConvert.EncodeName("My Post"), "600");

Special characters in XML?  (1617 Views)