Reputation: 1261
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
Upvotes: 3
Views: 8748
Reputation: 161
You should replace following illegal characters with:
<
(<)
&
(&)
>
(>)
"
(")
'
(')
Upvotes: 1
Reputation: 1764
You should use best practices as suggested by JTMon.
I also ran into same situation when exporting to XML. I dont know whether it would work for you or not but try using stringbuilder
instead string
to generate xml.
You can try this too
i.SubItems[0].Text.Trim('\0')
Hope it would help.
Upvotes: -1
Reputation: 2014
You may use even this "incriminated" characters if you wish using CDATA
Upvotes: 0
Reputation: 3199
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.
Upvotes: 3