采用序列化XmlSerializer的整数数组 [英] Serializing an array of integers using XmlSerializer

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

问题描述

我遇到一个问题,而试图通过XmlSerializer的一个XNA项目我工作的序列化整数的多尺寸阵列。我能够顺利序列化我所有的其他数据(布尔值,字符串,甚至颜色等)。我也看到很多人宣称的XmlSerializer将本地处理整数(单一尺寸)阵列,以及。有没有关于多尺寸阵列的限制,或者是另一回事怎么回事?

I'm encountering a problem while trying to serialize a multi-dimensioned array of integers via XmlSerializer for an XNA project I'm working on. I'm able to serialize all of my other data (booleans, strings, even Colors, etc) without a hitch. I've also seen plenty of people claim that XmlSerializer will natively handle (single-dimensioned) arrays of integers as well. Is there a limitation regarding multi-dimensioned arrays, or is something else going on here?

下面是相关的code:

Here's the relevant code:

int[,,] scoredata = scores;  // Populated with data elsewhere 

filename = Path.Combine(container.Path, "scoredata.sav"); 
stream = File.Open(filename, FileMode.Create); 
serializer = new XmlSerializer(typeof(int[,,])); 
serializer.Serialize(stream, scoredata);  // This line throws the exception. 
stream.Close();

我收到的例外是'System.InvalidOperationException'类型的未处理的异常发生在system.xml.dll的。有一个错误生成XML文档。

The exception I receive is "An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll. There was an error generating the XML document."

我也试着用这个数组作为一个成员变量的结构(这里我所有的其他球员数据存储),但我做的事情时,这样一来,也得到了同样的异常,这使我相信,它不是一个简单的语法错误之类的东西。

I've also tried using this array as a member variable in a struct (where all of my other player data is stored) but I get the same exception when doing things that way, as well, which leads me to believe that it's not a simple syntax error or anything like that.

我是否需要调整我的code通过一个单一尺寸阵列序列化,或者是有什么我俯瞰?

Do I need to restructure my code to serialize via a single-dimensioned array, or is there something I'm overlooking?

在此先感谢!

推荐答案

读取内异常:

  • 有一个错误反映类型'SOMETYPE。无法序列成员SomeType.Data类型System.Int32的[,,]',看到内部异常的更多细节。
  • 在无法序列类型System.Int32的[,,]的对象。不支持多维数组。

因此​​,没有:多维数组根本不支持。您可能需要通过向垫片它作为一维数组...你可以有一个单独的属性,做翻译做到这一点:

So no: multi-dimensional arrays simply aren't supported. You may have to shim it through as a single-dimension array... you can do this by having a separate property that does the translation:

[XmlIgnore]
public int[, ,] Data { get; set; }

[XmlElement("Data"), Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public int[] DataDto
{
    get { /* flatten from Data */ }
    set { /* expand into Data */ }
}

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

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