title: XML (de)serialization invalid string inconsistent in c#?
body: <p>In C# (.net 4.0 and 4.5 / vs2010 and vs12) when I serialize an object containing a string having an illegal character using XMLSerializer, no error is thrown. However, when I deserialize that result, an "invalid character" error is thrown.</p>
  
  <pre><code>        // add to XML
          Items items = new Items();
          items.Item = "\v hello world"; // contains "illegal" character \v
  
          // variables
          System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Items));
          string tmpFile = Path.GetTempFileName();
  
          // serialize
          using (FileStream tmpFileStream = new FileStream(tmpFile, FileMode.Open, FileAccess.ReadWrite))
          {
              serializer.Serialize(tmpFileStream, items);
          }
          Console.WriteLine("Success! XML serialized in file " + tmpFile);
  
          // deserialize
          Items result = null;
          using (FileStream plainTextFile = new FileStream(tmpFile, FileMode.Open, FileAccess.Read))
          {
              result = (Items)serializer.Deserialize(plainTextFile); //FAILS here
          }
  
          Console.WriteLine(result.Item);
  </code></pre>
  
  <p>"Items" is just a small class autogenerated by xsd /c Items.xsd. Items.xsd is nothing more than a root element (Items) containing one child (Item):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"&gt;
      &lt;xs:element name="Items"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element name="Item" type="xs:string" /&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>The error thrown during deserialization is</p>
  
  <blockquote>
    <p>Unhandled Exception: System.InvalidOperationException: There is an
    error in XML document (3, 12). ---> System.Xml.XmlException: '♂',
    hexadecimal value 0x0B, is an invalid character. Line 3, position 12.</p>
  </blockquote>
  
  <p>The serialized XML file contains on line 3 this:</p>
  
  <pre><code>&lt;Item&gt;&amp;#xB; hello world&lt;/Item&gt;
  </code></pre>
  
  <p>I know \v -> &amp; # xB; is an illegal character, but why does XMLSerialize allows it to be serialized (without error)? I find it inconsistent of .NET that it allows me to serialize something without a problem only to find out that I cannot deserialize it.</p>
  
  <p>Is there a solution so either the XMLSerializer removes the illegal characters automatically before serializing or can I instruct the deserialization to ignore the illegal characters?</p>
  
  <p>Currently I do solve it by reading the file contents as a string, replacing "manually" the illegal characters and next deserialize it... but I find that an ugly hack/work around.</p>