Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Second</strong> : A Solution</p> <p>Using the <code>DataContractSerializer</code> (which is used by default for WCF Services) instead of the <code>XmlSerializer</code> works a treat</p> <pre><code>[Serializable] public class MyData { public string Text { get; set; } } class Program { public static void Main(string[] args) { var myData = new MyData { Text = "hello " + ASCIIEncoding.ASCII.GetString(new byte[] { 0x12 }) + " world" }; var serializer = new DataContractSerializer(typeof(MyData)); var mem = new MemoryStream(); serializer.WriteObject(mem, myData); mem.Seek(0, SeekOrigin.Begin); MyData myData2 = (MyData)serializer.ReadObject(mem); Console.WriteLine("myData2 {0}", myData2.Text); } } </code></pre> <p><strong>Frist</strong> : A Workaround</p> <p>I can get it to choke when writing the Xml, by using an XmlWriter, which is arguably better than the client choking on it. e.g.</p> <p>However it doesn't fix the underlying problem of sending the invalid characters</p> <pre><code>[Serializable] public class MyData { public string Text { get; set; } } class Program { public static void Main(string[] args) { var myData = new MyData {Text = "hello " + ASCIIEncoding.ASCII.GetString(new byte[] { 0x12 }) + " world"}; var serializer = new System.Xml.Serialization.XmlSerializer(typeof(MyData)); var sw = new StringWriter(); XmlWriterSettings settings = new XmlWriterSettings(); using (var writer = XmlWriter.Create(sw)) { serializer.Serialize(writer, myData); // Exception // hexadecimal value 0x12, is an invalid character } var xmlReader = new StringReader(sw.ToString()); var newUser = (MyData)serializer.Deserialize(xmlReader); Console.WriteLine("User Name = {0}", newUser); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload