3

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>&#x8;Alimentação Controlador</Name> <Value>24</Value> <Unit>Vca</Unit> </Propery> 

Document is serialized with ASCIIEncoding.UTF8

4
  • Which encoding does the XML document declare? How do you produce the XML (do you use an XML library, or do you generate it directly)? Commented Sep 5, 2012 at 8:45
  • In your case & is the problem. Commented Sep 5, 2012 at 8:49
  • Document declares <?xml version="1.0" encoding="utf-8"?> Commented Sep 5, 2012 at 8:50
  • 1
    @FlorimMaxhuni No, the problem is the BS (Backspace, code 0x08) character, which is invalid in XML 1.0 Commented Nov 13, 2018 at 11:51

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This will not help, as the problem is the &#x8; character, which will be left untouched by the SecurityElement.Escape method.
1

You should replace following illegal characters with:

&lt; (<)

&amp; (&)

&gt; (>)

&quot; (")

&apos; (')

Comments

0

You may use even this "incriminated" characters if you wish using CDATA

look here

Comments

-1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.