Stach Posted December 29, 2012 Author Share Posted December 29, 2012 How do you go about this? I'm directing this towards Budweiser as he's been posting tutorials for XML and such but anyone can answer if you know an efficient way to do it. This is the only thing that is stumping me. Link to comment Share on other sites More sharing options...
Mal Posted December 29, 2012 Share Posted December 29, 2012 You serialize a class or you build an XDocument manually and add the array as an element or attribute. Either way I haven't done either so I'll work on both an array and a vector and get back to you.Edit: Alright, it looks like 2 dimensional arrays are not a supported type for serializing to XML. I'll look into vector's for ya later.Edit2: Vector2's (and I also assum Vector3's) are a supported type. Here's the source:```using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Xml.Serialization;using System.IO;// For Vector2'susing Microsoft.Xna.Framework;namespace Serializing2DArrayToXML{ class Program { static void Main(string[] args) { Array2D testArray = new Array2D(); // 2 dimensional arrays are not a supported type //testArray.Array[0, 0] = 1; //testArray.Array[1, 1] = 5; //testArray.Array[2, 2] = 10; // Vector's are testArray.VectorArray[0] = new Vector2(0, 1); testArray.VectorArray[1] = new Vector2(1, 1); testArray.VectorArray[2] = new Vector2(2, 2); XmlSerializer write = new XmlSerializer(testArray.GetType()); using (StreamWriter file = new StreamWriter("2darrays.xml")) write.Serialize(file, testArray); } } public class Array2D { //[XmlElement("Array Indexes")] //public int[,] Array { get; set; } [XmlElement("Vector Array")] public Vector2[] VectorArray { get; set; } public Array2D() { // Initialization // Array = new int[10,10]; VectorArray = new Vector2[10]; } }}``` Link to comment Share on other sites More sharing options...
Stach Posted December 30, 2012 Author Share Posted December 30, 2012 So say you are designing the game maps. Since there is no way of writing a multidimensional array to a binary or XML file, how do you go about saving the tile data? Link to comment Share on other sites More sharing options...
Mal Posted December 30, 2012 Share Posted December 30, 2012 Use a BinaryFormatter and serialize the array.```using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;// For the FileStream and BinaryFormatter classesusing System.IO;using System.Runtime.Serialization.Formatters.Binary;namespace Serializing2DArrayToBinary{ class Program { static void Main(string[] args) { int[,] array2D = new int[10, 10]; array2D[0, 0] = 5; array2D[1, 1] = 10; array2D[2, 2] = array2D[0, 0] + array2D[1, 1]; // Serialize the array BinaryFormatter bf = new BinaryFormatter(); using (FileStream file = new FileStream("array.dat", FileMode.Create, FileAccess.Write)) bf.Serialize(file, array2D); // Deserialize and print int[,] anotherArray; using (FileStream file = new FileStream("array.dat", FileMode.Open, FileAccess.Read)) anotherArray = (int[,])bf.Deserialize(file); Console.WriteLine(anotherArray[2, 2]); } }}``` Link to comment Share on other sites More sharing options...
Stach Posted December 31, 2012 Author Share Posted December 31, 2012 Thank you. You've helped solve my problem. ![:)](http://www.touchofdeathforums.com/community/public/style_emoticons/<#EMO_DIR#>/smile.png) Link to comment Share on other sites More sharing options...
Mal Posted December 31, 2012 Share Posted December 31, 2012 > Thank you. You've helped solve my problem. ![:)](http://www.touchofdeathforums.com/community/public/style_emoticons/<#EMO_DIR#>/smile.png)I don't think "helped to solve" is quite the right phrase to use when I wrote the code for ya, but it's all good; you're quite welcome, and I hope you can use it for whatever purpose. Link to comment Share on other sites More sharing options...
Stach Posted January 1, 2013 Author Share Posted January 1, 2013 I didn't use the code you gave me, I used the BinaryFormatter. I'm saying thank you for directing me to that. Link to comment Share on other sites More sharing options...
Mal Posted January 1, 2013 Share Posted January 1, 2013 > I didn't use the code you gave me, I used the BinaryFormatter. I'm saying thank you for directing me to that.And I wrote a fully working example. Anyway. Mark this as resolved, please. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now