List错误的序列化

编程入门 行业动态 更新时间:2024-10-28 14:26:13
本文介绍了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="www.w3/2001/XMLSchema-instance" xmlns:xsd="www.w3/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序列化类必须只包含一个 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错误的序列化

本文发布于:2023-10-25 14:37:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1527244.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   序列化   List

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!