List错误的序列化 [英] serialization of List error

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

问题描述

given the following code 
[Serializable]
    public class check
    {
        public check()
        {
            //int_array = new List<int>();
            string_array = new List<string>();
        }

        [XmlArray("string_array",Order=1)]
        [XmlArrayItem("string_element",typeof(string))]
        public List<string> string_array;

        //[XmlArray("int_array")]
        //[XmlArrayItem("int_element")]
        //public List<int> int_array;
        public void write_to_xml(string path)
        {
            XmlSerializer serailizer = new XmlSerializer(typeof(check));
            TextWriter writer = new StreamWriter(path);
            serailizer.Serialize(writer, this);
            writer.Close();
        }
        static void Main()
        {
           //check class
            check c = new check();
            c.string_array.AddRange(new string[] { "string1", "string2", "string3" });
            c.write_to_xml(@"E:\check.xml");
        }
    }





此代码运行正常并生成XML文件



this code run OK and XML file generated is

<?xml version="1.0" encoding="utf-8"?>
<check xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <string_array>
    <string_element>string1</string_element>
    <string_element>string2</string_element>
    <string_element>string3</string_element>
  </string_array>
</check>



现在删除上一课中的评论并再次点击F5

发生了什么

此错误出现反映类型''FET.check''时出现错误。

为什么会出现此错误?

IS序列化类必须只包含一个< b> XmlArray 属性?或什么?

推荐答案

Hello Ibrahim,



只需更改int List上的注释,如下所示。

Hello Ibrahim,

Just change the annotations on int List as shown below.
[XmlArray("int_array", Order=2)]
[XmlArrayItem("int_element", typeof(int))]
public List<int> int_array;</int>





问候,



Regards,


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

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