Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[C#] Read/write 2 dimensional arrays into an XML file


Stach
 Share

Recommended Posts

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's

using 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

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 classes

using 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

> 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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...