I find XmlSerializer cannot handle a char correctly when serialize/deserialize an object, sorry I cannot type it here, please see below image.
Does anybody know how to handle such a char when serialize/deserialize it?
Thanks a lot!
My code sample: An entity class definition:
public class Entity
{
public string value;
public override string ToString()
{
return string.Format("Entity.Value: {0}", this.value);
}
}
Create an XmlSerializer object based on the entity type:
private XmlSerializer GetXmlSerializer()
{
xmlSerializer = default(XmlSerializer);
Type type = typeof(Entity);
IEnumerable<Type> types = new List<Type> { type };
xmlSerializer = new XmlSerializer(type, types.ToArray());
return xmlSerializer;
}
Code to serialize an Entity object:
private void BT_Serialize_Click(object sender, EventArgs e)
{
using (StringWriter sw = new StringWriter(System.Globalization.CultureInfo.InvariantCulture))
{
xmlSerializer.Serialize(sw, entity);
string result = sw.ToString();
}
}
Code to deserialize an Entity object based on the XML serialized (I can change it manually):
private void BT_Deserialize_Click(object sender, EventArgs e)
{
using (StringReader sr = new StringReader(xml))
{
object obj = xmlSerializer.Deserialize(sr);
Entity entity = obj as Entity;
}
}