我有一个非常基本的类,它是子类的列表,加上一些摘要数据。
[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。
我做错什么了吗?这是怎么回事??
UPDATE感谢您的回复。我不知道这是设计使然。我已经转换为 BinaryFormatter (用于二进制序列化)并且它工作得非常好。
下列的msdn http://msdn.microsoft.com/en-us/library/ms950721.aspx:
问:为什么集合类的所有属性没有被序列化?
答:XmlSerializer 仅在检测到 IEnumerable 或 ICollection 接口时才序列化集合中的元素。此行为是设计使然。唯一的解决方法是将自定义集合重构为两个类,其中一个类公开包括一种纯集合类型的属性。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)