[英]Serializing an array of integers using 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.我在尝试通过XmlSerializer
为我正在处理的 XNA 项目序列化多维整数数组时遇到问题。 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.我还看到很多人声称XmlSerializer
也可以本地处理(单维)整数数组。 Is there a limitation regarding multi-dimensioned arrays, or is something else going on here?多维数组是否有限制,或者这里还有其他事情吗?
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();
The exception I receive is我收到的例外是
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll. System.Xml.dll 中发生类型为“System.InvalidOperationException”的未处理异常。 There was an error generating the XML document.生成 XML 文档时出错。
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.我还尝试将此数组用作结构中的成员变量(存储我所有其他玩家数据的位置),但是在以这种方式做事时我也遇到了相同的异常,这让我相信它不是一个简单的语法错误或类似的错误。
Do I need to restructure my code to serialize via a single-dimensioned array, or is there something I'm overlooking?我是否需要重构我的代码以通过单维数组进行序列化,或者我忽略了什么?
Thanks in advance!提前致谢!
Read the inner-exceptions:阅读内部异常:
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 */ }
}
It took me a while to figure out what should go into Marc's get and set braces to flatten and expand multi-dimensional arrays.我花了一段时间才弄清楚应该在 Marc 的 get 和 set 大括号中加入什么来展平和扩展多维数组。
Here is my solution for 2D arrays.这是我的二维数组解决方案。
In my case, I know at compile time that one of the dimensions is 4 so I did not have to store (somehow) the array dimensions.就我而言,我在编译时知道其中一个维度是 4,因此我不必(以某种方式)存储数组维度。
[XmlIgnore]
public int[,] Readings { get; set; }
[XmlArray("Readings")]
public int[] ReadingsDto {
get { return Flatten(Readings); }
set { Readings = Expand(value, 4); }
}
public static T[] Flatten<T>(T[,] arr)
{
int rows0 = arr.GetLength(0);
int rows1 = arr.GetLength(1);
T[] arrFlattened = new T[rows0 * rows1];
for (int j = 0; j < rows1; j++)
{
for (int i = 0; i < rows0; i++)
{
var test = arr[i, j];
arrFlattened[i + j * rows0] = arr[i, j];
}
}
return arrFlattened;
}
public static T[,] Expand<T>(T[] arr, int rows0)
{
int length = arr.GetLength(0);
int rows1 = length / rows0;
T[,] arrExpanded = new T[rows0, rows1];
for (int j = 0; j < rows1; j++)
{
for (int i = 0; i < rows0; i++)
{
arrExpanded[i, j] = arr[i + j * rows0];
}
}
return arrExpanded;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.