Serialization of unprintable character

The following code;

var c = (char) 1;

var serializer = new XmlSerializer(typeof (string));

var writer = new StringWriter();
serializer.Serialize(writer, c.ToString()); 
var serialized = writer.ToString();

var dc = serializer.Deserialize(new StringReader(serialized));

Throws this exception in .NET 4.

Invalid Operation Exception – There is an error in XML document (2, 12). ”, hexadecimal value 0x01, is an invalid character. Line 2, position 12

Am I doing something wrong? Or is there a reasonable work around?

Many thanks!

XML Deserialization of carriage returns causes unprintable characters

Situation: When I deserialize XML that contains carriage returns, the characters appear as unprintable character boxes rather than as carriage returns. Background: User input collected via a multi-l

How to detect this unprintable character using regex or other ways in Ruby?

I came upon a strange character (using Nokogiri). irb(main):081:0> sss.dump => ///u{a0}/ irb(main):082:0> puts sss => nil irb(main):083:0> sss => irb(main):084:0> sss

Using cut with unprintable delimiters

Is it possible to use cut and have unprintable characters be the delimiter? For example I’d like to have the ^A characters (also represented as /001) be the delimiter.

Unprintable characters in MySQL

I want to test if a certain BLOB starts with character 255 (/xff). Is there a way to encode this character in a literal string? Table 9.1. Special Character Escape Sequences gives certain special char

PHP: Remove unprintable characters without removing white space

I know this a common question but everything I found seems to remove white space. I’m looking for a regular expression that will strip unprintable characters WITHOUT changing any whitespace. This a fu

Java – Remove unprintable character from string

I was trying to pass a string into a SQL statement that has a newline, so it threw me an error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for

Xml serialization appending the /0 (backslash 0 or Null) character?

I am trying to serialize an object and the /0 (Null) character is being appended to the generated string. Why? My code: XmlSerializer serializer = new XmlSerializer(typeof(Common.PlanogramSearchOptio

Cygwin sed: search/replace one single unprintable char using hex, octal or decimal

sorry for the simple question, but I have gone blind for four days studying and trying, and can’t seem to strike the right syntax. Using sed on cygwin, I am trying to replace one single unprintable AS

Why does my json string have 28 unprintable characters in it?

I am producing a JSON string from a database query in JSP but it always has unprintable characters in it and I don’t understand why! The JSP to produce the JSON is below: String user = username; //

How to create special character string in

In ancient time, we can specify all characters with chr(56) For example, say the character is unprintable. We want to put it in a string. Just do Dim a as string = chr (56) Now we have UTF8 or unicod


You’re trying to serialize characters which can’t be represented within XML. Unfortunately they break XML serialization. I don’t know of any workarounds for this other than writing your own escaping code.

On the other hand, actual uses for such characters (ASCII characters before space, other than tab, carriage return and line feed IIRC) are relatively rare – you may find you’re okay just to strip them. Alternatives are to come up with your own escaping, or encode the whole string as binary and base64 the result. Escaping will take a good deal less space than the re-encoding approach 🙂

There is a workaround as explained here – you can use XmlReaderSettings.CheckCharacters option to ignore validation of characters:

XmlReader xr = XmlReader.Create(new StringReader(serialized),
    new XmlReaderSettings { CheckCharacters = false });
var dc = (string)serializer.Deserialize(xr);