I use the following code to deserialize XML string to an object of type T:
public static T DeserializeXMLToObject<T>(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);
}
}
But it throws an exception when it accepts an invalid hexadecimal value :
XMLException: There is an error in XML document (217388, 15). ‘[]’, hexadecimal value 0x1A, is an invalid character. Line 217388, position 15.
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
In what encoding should I change the line new UnicodeEncoding().GetBytes(xmlText)) into that will accept any hexadecimal value?
My code is in C#, framework 4, build in VS2010 Pro.
There is no such encoding. Those characters are simply illegal in XML.
If you want to serialize binary data, then you should convert it to base64 or hex or something.