title: C# Encoding that Allow Any Hexadecimal Value
body: <p>I use the following code to deserialize XML <code>string</code> to an <code>object</code> of type <em><code>T</code></em>:</p>
  
  <pre><code>public static T DeserializeXMLToObject&lt;T&gt;(string xmlText)
  {
      if (string.IsNullOrEmpty(xmlText)) return default(T);
      XmlSerializer xs = new XmlSerializer(typeof(T));
      using (MemoryStream memoryStream = new MemoryStream(new UnicodeEncoding().GetBytes(xmlText)))
      using (XmlTextReader xsText = new XmlTextReader(memoryStream))
      {
          xsText.Normalization = true;
          return (T)xs.Deserialize(xsText);
      }
  }
  </code></pre>
  
  <p>But it throws an exception when it accepts an invalid hexadecimal value :</p>
  
  <blockquote>
    <p><strong>XMLException</strong>: There is an error in XML document (217388, 15). '[]', hexadecimal value 0x1A, is an invalid character. Line 217388, position 15.</p>
    
    <p><em>at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)</em></p>
    
    <p><em>at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)</em></p>
  </blockquote>
  
  <p><strong>In what encoding should I change the line</strong> <code>new UnicodeEncoding().GetBytes(xmlText))</code> <strong>into that will accept any hexadecimal value?</strong></p>
  
  <p>My code is in C#, framework 4, build in VS2010 Pro.</p>