Try saving the xml data as shielded data using the SecurityElement from the System.Security namespace. More information can be found here.
string xmlData = SecurityElement.Escape(xmlData);
Invalid XML characters can be written by creating an XDocument with the XmlWriterSettings parameter set with the CheckCharacters property to false. This will then replace them with a numeric character, such as # 0; - and # 0x1F. See this article for more details. Alternatively, you can call some xml cleanup method, like this one.
Dmitry Savy
source share