【发布时间】:2013-04-30 21:01:27
【问题描述】:
有没有办法在 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 序列化都特别陌生,因此我们将不胜感激。
【问题讨论】:
-
Data可能不是唯一的,因为它可能在多个使用的命名空间中定义。请在代码中显示您的using语句。 -
我的代码和我的程序一样准确。将数组更改为单个字符串并缩短它可以正常工作并将数据写入文件 - 编辑误读,将添加。
-
xmlserializer 不支持多维数组。
-
还有其他方法吗?
-
@evanmcdonnal
[][]和[,]是完全不同的两个东西。
标签: c# xml xml-serialization