Using XElement to create html in code behing. Reusing the existing code with new data, the XElement gives this error trying to access the ToString() method. Obviously the problem was that the new data contains 0x1F character.
From the blog, http://seattlesoftware.wordpress.com/2008/09/11/hexadecimal-value-0-is-an-invalid-character/:
The decimal range for ASCII control characters is 0 – 31, and 127. Or, in hex, 0×00 – 0x1F. The problem that causes these XmlExceptions is that the data being read or loaded contains characters that are illegal according to the XML specifications. Almost always, these characters are in the ASCII control character range (think whacky characters like null, bell, backspace, etc). These aren’t characters that have any business being in XML data; they’re illegal characters that should be removed, usually having found their way into the data from file format conversions, like when someone tries to create an XML file from Excel data, or export their data to XML from a format that may be stored as binary.
This is a very accurate match for this case. The text which contains this character is OCR read from paper as the only source.
The solution was to manually replace this character with a white space, before adding the value to the XElement.
.Replace((char)(0x1F), ' ')
Wish I could do this more generic, with a framework Encode method.