0
我有兩個類,其中一個是從另一個繼承的。XmlSerializer自定義序列化
例如Animal這是一個基類,Dog是一個繼承類。
我填充了一個List集合,我想將它序列化爲List集合。
可能嗎?
string serializedResult = XmlSerializeHelper<List<Animal>>.Serialize(result.ToList());
Where result is a List<Dog> collection.
而且XmlSerializeHelper實現如下:
public class XmlSerializeHelper<T> where T : class
{
public static string Serialize(object o)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
StringWriter stringWriter = new StringWriter();
xmlSerializer.Serialize(stringWriter, o);
return stringWriter.ToString();
}
public static T Deserialize(string xml)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
StringReader stringReader = new StringReader(xml);
return (T)xmlSerializer.Deserialize(stringReader);
}
}