XmlSerializer的并不在我的课都序列化 [英] XmlSerializer doesn't serialize everything in my class

查看:143
本文介绍了XmlSerializer的并不在我的课都序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的类,它的子类的列表,再加上一些汇总数据。

I have a very basic class that is a list of sub-classes, plus some summary data.

[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()我得到了以下内容:

When I call SaveProductCollection() I get the following:

<?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>



注意,我有基本类型:列表<产品与GT; 。但是,我没有任何的类属性:旗,A,B,C

Note that I have the base type : List<Product>. But I don't have any of the class properties: flag, A, B, C.

难道我做错了什么?这是怎么回事?

Did I do something wrong? What's up??

更新的答复感谢。我不知道这是特意设计的。我已经转换为的BinaryFormatter(二进制序列化,而不是),它奇妙的作品

UPDATE Thanks for the replies. I wasn't aware that it was by-design. I've converted to BinaryFormatter (for binary serialization instead) and it works wonderfully.

推荐答案

以下的msdn

问:为什么不收集的所有属性类序列化?

Q: Why aren't all properties of collection classes serialized?

答:XmlSerializer的,只有当它检测到不是了IEnumerable或ICollection接口序列化集合中的元素。此行为是设计使然。唯一的解决办法是重新因数自定义集合分为两类,其中一个公开性质包括纯集合类型之一。

A: The XmlSerializer only serializes the elements in the collection when it detects either the IEnumerable or the ICollection interface. This behavior is by design. The only work around is to re-factor the custom collection into two classes, one of which exposes the properties including one of the pure collection types.

这篇关于XmlSerializer的并不在我的课都序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆