I have a .NET application written in C# that saves information in XML format. The application serializes the application data into xml. The application contains a textbox where the user is free to enter any text. I have cases where users have managed to enter characters, mostly when they copy and paste text from other applications into the textbox, where the xml document becomes corrupted. Is there a general approach to manage illegal characters in xml documents without having to filter them out in every textbox on entry.
Sample from document thats corrupted
<Propery>
<Name>Alimentação Controlador</Name>
<Value>24</Value>
<Unit>Vca</Unit>
</Propery>
Document is serialized with ASCIIEncoding.UTF8
I think your best bet is to “SafeEncode” the string entered by the user. this link http://msdn.microsoft.com/en-us/library/system.security.securityelement.escape(VS.80).aspx shows you how to do it easily with one call to the SecurityElement.Escape(string s) method.