   [XNA] How can I serialize multidimensionsal arrays to an XML file?
  Hi folks,I would like to save my current level layout, which is stored in a 3 dimensional array, into a .XML file.The problem is, when I use the following:   private void XMLSave(object sender, System.EventArgs e)   {  object testValue = Game1.Instance.level3D.map3D;  XmlWriterSettings settings = new XmlWriterSettings();  settings.Indent = true;  using (XmlWriter xmlWriter = XmlWriter.Create("LevelMap.xml", settings))  { IntermediateSerializer.Serialize(xmlWriter, testValue, null);  }   }I get the following error:"Cannot serialize multidimensional arrays."How can I avoid this error?  One solution I thought of was to convert the enitre 3D array into a 1D one before saving.  Is this sensible?  Can it be done?  Perhaps you guys have some better suggestions?Thank you.   
  Yeah, converting into a 1D array is quite sensible. Although it would probably be better to just convert your game object so that it uses a 1D array, with a bunch of accessor functions.Alternativly if you have a lot of multi dimensional arrays it is not to hard to write a serializer which will serialize an array of arbitary dimensions. This would use a recursive function and the rank of the array. An example(from my own serializer, but it would be similar with the XNA serializer):   private void WriteArrayRec(VersionedSerializer serializer, Array array, int currentRank, int[] arrayIndices)   {  if (currentRank >= array.Rank)  { object value = array.GetValue(arrayIndices); serializer.WriteObject(value);  }  else  { for (int i = array.GetLowerBound(currentRank); i <= array.GetUpperBound(currentRank); i++) {arrayIndices[currentRank] = i;WriteArrayRec(serializer, array, currentRank + 1, arrayIndices); }  }   };Quote:Original post by dblackYeah, converting into a 1D array is quite sensible. Although it would probably be better to just convert your game object so that it uses a 1D array, with a bunch of accessor functions.Thanks dude.  I thought about having my level layout as a 1D array but how would I work out the x, y and z bounds using a 1D array?  For example differentiating between the end of one row and the start of another.  The only way I could think of would be to use modulo arithmetic, and wouldn't that be slow as heck?  The save process isn't going to be called each frame so it makes sense to convert there and keep the array in a more managable format; from my point of view.This is what I have created for converting the 3D array to a 1D array:   private void XMLSave(object sender, System.EventArgs e)   {  int[,,] level = Game1.Instance.level3D.map3D;  int width = level.GetLength(0);  int height = level.GetLength(1);  int depth = level.GetLength(2);  int[] array1D = new int[width * height * depth];  for (int x = 0; x < width; ++x)  { for (int y = 0; y < height; ++y) {for (int z = 0; z < depth; ++z){    array1D[x * width * height + y * width + z] = level[x, y, z];} }  }  object testValue = array1D;  XmlWriterSettings settings = new XmlWriterSettings();  settings.Indent = true;  using (XmlWriter xmlWriter = XmlWriter.Create("LevelMap.xml", settings))  { IntermediateSerializer.Serialize(xmlWriter, testValue, null);  }   } If there are any obvious mistakes then please shout :)   ;  That looks OK.I dont understand what you mean about needing modulo arithmetic, you just use the array the same as how you accessed it when doing the conversion. To reduce typing you can overload the [int,int,int] operator for your level object. The compiler would generate similar code for array accesses anyway. You can even add some asserts or exceptions to check for invalid indices on each dimension.While it is true there might be a tiny performance cost for this(ie the compiler/JIT has a harder time eliminating bounds checks from loops), it is doubtful that it would make an overall difference. But measure and see, as always.David   ;Quote:Original post by dblackI dont understand what you mean about needing modulo arithmetic, you just use the array the same as how you accessed it when doing the conversion. In a 3D array, if I wanted to check to see if an adjacent node exists I do the following:  // Check if a node exists to the left  if (x - 1 >= 0)  // Check if a node exists to the right  if (x + 1 < width)  // Check if a node exists below  if (y - 1 >= 0)  // Check if a node exists above  if (y + 1 < height)...etcI can't use the same process for a 1D array as there is only one dimension.  For example if I were to check for an adjacent node at the end of a row it would return a node when I would want it to return no node.Does that make sense?How can I overcome this situation without resorting to modulo arithmetic?   ;  Whats the difference?You still have a width, height, depth. But you effectivly emulate the 3D array with a 1D one, everything else except the indexing is the same.   ;Quote:You still have a width, height, depth. But you effectivly emulate the 3D array with a 1D one, everything else except the indexing is the same.You are absolutely right!  I was making things way too difficult for myself, and thinking about things in the wrong way (as usual) :)I only need to access the array index differently:[x * width * height + y * width + z] instead of [x, y, z]I think it might be safe to say that this problem has been solved.Thanks for your help.   