当一个类从列表&LT继承;>中的XmlSerializer不序列其他属性 [英] When a class is inherited from List<>, XmlSerializer doesn't serialize other attributes

查看:250
本文介绍了当一个类从列表&LT继承;>中的XmlSerializer不序列其他属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个情况下,我需要我的班从名单,其中继承;的ItemType> ,但是当我这样做的XmlSerializer不序列任何财产或领域在我的类中声明,在下面的示例说明:

 公共部分类Form1中:形态
{
    公共Form1中()
    {
        的InitializeComponent();
        DoSerialize();
    }
    私人无效DoSerialize()
    {
        MyClass的OBJ =新MyClass的();
        obj.Add(1);
        obj.Add(2);
        obj.Add(3);
        XmlSerializer的S =新的XmlSerializer(typeof运算(MyClass的));
        StringWriter的SW =新的StringWriter();
        s.Serialize(SW,OBJ);
    }
}
[可序列化]
[XmlRoot]
公共类MyClass的:列表< INT>
{
    公共MyClass的()
    {
    }
    INT myAttribute = 2011;
    [XmlAttribute]
    公众诠释MyAttribute
    {
        得到
        {
            返回myAttribute;
        }
        组
        {
            myAttribute =价值;
        }
    }
}
 

生成的XML:

 < XML版本=1.0编码=UTF-16&GT?;
< ArrayOfInt的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance的xmlns:XSD =htt​​p://www.w3.org/2001/XMLSchema>
  < INT> 1< / INT>
  &其中; INT→2&其中; / INT>
  &其中; INT→3&所述; / INT>
< / ArrayOfInt>
 

解决方案

这是设计使然。我不知道为什么这个决定是,但它在文档声明:

  
      
  • 类实现的ICollection或IEnumerable的。只有集合是   序列化,而不是公共属性。
  •   

(下查找选项,可以序列化部分)。 <一href="http://connect.microsoft.com/VisualStudio/feedback/details/105767/xmlserializer-cant-serialize-properties-of-type-ilist-t">Someone已经提出了对这一错误,但它不会改变 - 在这里,微软也证实,不包括属性类实施的ICollection 实际上是在XmlSerializer的行为。

一个解决方法是两种:

  • 实施的IXmlSerializable 和控制序列化自己。

  • 更改MyClass的所以它有一个List类型的公共财产(不继承它)。

  • 使用的DataContractSerializer,处理这种情况。

I'm having a situation here, I need my class to be inherited from List<ItemType>, but when I do this XmlSerializer does not serialize any property or field declared in my class, the following sample demonstrates:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        DoSerialize();
    }
    private void DoSerialize()
    {
        MyClass obj = new MyClass();
        obj.Add(1);
        obj.Add(2);
        obj.Add(3);
        XmlSerializer s = new XmlSerializer(typeof(MyClass));
        StringWriter sw = new StringWriter();
        s.Serialize(sw, obj);
    }
}
[Serializable]
[XmlRoot]
public class MyClass : List<int>
{
    public MyClass()
    {
    }
    int myAttribute = 2011;
    [XmlAttribute]
    public int MyAttribute
    {
        get
        {
            return myAttribute;
        }
        set
        {
            myAttribute = value;
        }
    }
}

the resulting XML:

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfInt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <int>1</int>
  <int>2</int>
  <int>3</int>
</ArrayOfInt>

解决方案

This is by design. I don't know why this decision was made, but it is stated in the documentation:

  • Classes that implement ICollection or IEnumerable. Only collections are serialized, not public properties.

(Look under "Items that can be serialized" section). Someone has filed a bug against this, but it won't be changed - here, Microsoft also confirms that not including the properties for classes implementing ICollection is in fact the behaviour of XmlSerializer.

A workaround would be to either:

  • Implement IXmlSerializable and control serialization yourself.

or

  • Change MyClass so it has a public property of type List (and don't subclass it).

or

  • Use DataContractSerializer, which handles this scenario.

这篇关于当一个类从列表&LT继承;&gt;中的XmlSerializer不序列其他属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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