我有一个非常基本的类,它是一个子类列表,外加一些摘要数据。
[Serializable]
public class ProductCollection : List<Product>
{
public bool flag { get; set; }
public double A { get; set; }
public double B { get; set; }
public double C { get; set; }
}
// method to save this class
private void SaveProductCollection()
{
// Export case as XML...
XmlSerializer xml = new XmlSerializer(typeof(ProductCollection));
StreamWriter sw = new StreamWriter("output.xml");
xml.Serialize(sw, theCollection);
sw.Close();
}
当我调用 SaveProductCollection()我得到以下信息:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfProduct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Product>
<InputType>1</InputType>
</Product>
<Product>
<InputType>1</InputType>
</Product>
</ArrayOfProduct>
请注意,我有基本类型:List<Product> .但是我没有任何类属性:flag、A、B、C。
我做错了什么吗?怎么了??
更新 感谢您的回复。我不知道这是设计使然。我已经转换为 BinaryFormatter(代替二进制序列化)并且效果非常好。
请您参考如下方法:
正在关注 msdn :
问:为什么集合类的所有属性都没有序列化?
A:XmlSerializer 仅在检测到 IEnumerable 或 ICollection 接口(interface)时才序列化集合中的元素。此行为是设计使然。唯一的解决方法是将自定义集合重构为两个类,其中一个公开包含纯集合类型之一的属性。
