加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在C#中序列化数组

发布时间:2020-12-16 01:54:41 所属栏目:百科 来源:网络整理
导读:有没有办法在C#中序列化整个数组,例如: [Serializable()]public class Data{ public short Some_Short_Data = new short[100,100]; public string Some_String_Data = new string[100,100];} 然后写文件: using System;using System.Collections.Generic;u
有没有办法在C#中序列化整个数组,例如:

[Serializable()]
public class Data
{
    public short Some_Short_Data = new short[100,100];
    public string Some_String_Data = new string[100,100];
}

然后写文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using System.Xml;
using System.Xml.Serialization;
using System.IO;

Data Write_Data = new Data();

XmlSerializer Writer = new XmlSerializer(typeof(Data));

    using (FileStream file = File.OpenWrite("Myfile.xml"))
    {
        Writer.Serialize(file,Write_Data); //Writes data to the file
    }

当我尝试这个时,它失败了:
?????XmlSerializer Writer = new XmlSerializer(typeof(Data));

说:“反映’数据’类型的错误”

我对Stack和Xml / Xml序列化都是新手,所以任何帮助都将不胜感激.

解决方法

XmlSerialzier不支持多维数组.但是你可以通过使用像这样的临时类来解决这个问题

public class Array100<T>
{
    public T[] Data = new T[100];
}

public class Data
{
    public Array100<short>[] Some_Short_Data = new Array100<short>[100];
    public Array100<string>[] Some_String_Data = new Array100<string>[100];
}

顺便说一句:不需要Serializable属性.它不被XmlSerialzier使用

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读