【问题标题】:Lost attributes in serialization List<> inherited class序列化 List<> 继承类中的丢失属性
【发布时间】:2013-09-12 13:28:34
【问题描述】:

我正在使用 WCF。我有以下模型类。当对象序列化列表cIntList 属性Name 丢失时。我在这里找到了答案: When a class is inherited from List<>, XmlSerializer doesn't serialize other attributes。 但是,对我来说重要的是不要构建容器类只修改相同的序列化。任何人都可以帮我修改该类以使其序列化符合我的期望吗?

   public class IntData
    {
        public int Value;
        public IntData()
        {
        }
    }

    public class cIntList : List<IntData>
    {
        public string Name;

        public cIntList()
        {
            Name = "Name";
            this.Add(new IntData() { Value = 1 });
            this.Add(new IntData() { Value = 2 });
        }
    }

【问题讨论】:

    标签: c# wcf serialization datacontractserializer xmlserializer


    【解决方案1】:

    如果您更改类,它也会序列化名称字段。

    public class cIntList
    {
        public string Name{ get; set; }
    
        [XmlElement("")]
        public List<IntData> IntList{ get; set; }
    
        public cIntList()
        {
            Name = "Name";
            IntList = new List<IntData>();
            IntList.Add(new IntData() { Value = 1 });
            IntList.Add(new IntData() { Value = 2 });
        }
    }
    

    您可以根据所需的 xml 更改或删除 XmlElement 属性。

    【讨论】:

    • 事实上,这可能是实现我的功能的最佳方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多