Well my friend the solution is far more simple than you realize:-
vbnet Code:
Public Class MapInfo
Private _colTiles As New Collections.ObjectModel.Collection(Of tilerec)
Public Property Tiles() As Collections.ObjectModel.Collection(Of tilerec)
Get
Return _colTiles
End Get
Set(ByVal value As Collections.ObjectModel.Collection(Of tilerec))
_colTiles = value
End Set
End Property
End Class
You can create a class like the above and use XML serialization to save and load:-
vbnet Code:
'
Public Function LoadMapInfo(ByVal fileName As String) As MapInfo
Dim ser As New XmlSerializer(GetType(MapInfo))
Dim fs As New FileStream(fileName, FileMode.Open)
Dim ret As MapInfo
ret = ser.Deserialize(fs)
fs.Close()
fs.Dispose()
Return ret
End Function
Public Sub SaveMapInfo(ByVal map As MapInfo, ByVal fileName As String)
Dim ser As New XmlSerializer(GetType(MapInfo))
Dim fs As New FileStream(fileName, FileMode.Create)
ser.Serialize(fs, map)
fs.Close()
fs.Dispose()
End Sub
The above functions can load and save the class to a file and load it back. Here is an example of their use:-
vbnet Code:
'
Dim mi As New MapInfo
mi.Tiles.Add(New tilerec With {.data = 12, .name = "Unknown", .x = 11, .y = 0})
mi.Tiles.Add(New tilerec With {.data = 11, .name = "Bing", .x = 12, .y = 4})
mi.Tiles.Add(New tilerec With {.data = 122, .name = "True", .x = 40, .y = 155})
mi.Tiles.Add(New tilerec With {.data = 3000, .name = "GRRR...!!", .x = 30, .y = 122})
SaveMapInfo(mi, "c:\MI.TXT")
Dim mi2 As MapInfo = LoadMapInfo("C:\MI.TXT")
The saved data would look like this:-
xml Code:
<?xml version="1.0"?>
<MapInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Tiles>
<tilerec>
<x>11</x>
<y>0</y>
<data>12</data>
<name>Unknown</name>
</tilerec>
<tilerec>
<x>12</x>
<y>4</y>
<data>11</data>
<name>Bing</name>
</tilerec>
<tilerec>
<x>40</x>
<y>155</y>
<data>122</data>
<name>True</name>
</tilerec>
<tilerec>
<x>30</x>
<y>122</y>
<data>3000</data>
<name>GRRR...!!</name>
</tilerec>
</Tiles>
</MapInfo>
However, games generally don't save their map data in a text format like Xml. A binary version of the same data would be far smaller in size and much faster in IO operations. You should consider that, its not absolutely necessary, its just more efficient. It should only take a mild alteration to the above code to make it serialize as a binary file instead of Xml.