I have an XML File with Elements that look like the following:
<level>
<name>Name of Level 1</name>
<number>1</number>
<authorTime>8.55</authorTime>
<scoringTime>20</scoringTime>
<map width="19" height="15"><!--Level1-->
<row>0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0</row>
<row>2,-1,-1,-1,-1,-1,-1,-1,2,-1,-1,-1,-1,-1,-1,-1,-1,-1,2</row>
<row>2,-1,-1,-1,0,1,0,-1,2,-1,0,-1,-1,-1,-1,-1,0,-1,2</row>
<row>0,1,0,-1,2,-1,-1,-1,2,-1,-1,-1,-1,-1,-1,-1,-1,-1,2</row>
<row>2,-1,-1,-1,2,-1,0,1,0,1,0,-1,0,0,-1,0,0,-1,2</row>
<row>2,-1,0,1,0,-1,-1,-1,2,-1,-1,-1,-1,2,-1,-1,2,-1,2</row>
<row>2,-1,-1,3,2,-1,0,-1,2,-1,0,-1,-1,2,-1,-1,2,-1,2</row>
<row>2,-1,0,1,0,0,0,-1,0,-1,0,1,1,0,-1,-1,2,-1,2</row>
<row>0,1,0,-1,-1,2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,2,-1,2</row>
<row>2,-1,2,-1,-1,0,1,0,1,0,-1,0,1,0,1,1,0,-1,2</row>
<row>2,-1,0,0,-1,-1,-1,0,-1,2,-1,2,-1,0,-1,-1,2,-1,2</row>
<row>2,-1,2,-1,-1,0,-1,-1,-1,2,-1,2,-1,-1,-1,-1,2,-1,2</row>
<row>2,-1,0,0,-1,0,1,1,1,0,1,0,-1,0,1,1,0,-1,2</row>
<row>2,5,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,2</row>
<row>0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0</row>
</map>
</level>
And I want to use an XDocument (in C#/XNA) to read the “map” into a 2D rectangular array (y,x) – so in this example case it would be int[15,19].
All I can think of is creating a jagged array and converting to a rectangular one later – something like
int[][] test = ((from level in xDoc.Descendants("level")
select (from map in level.Element("map")
select (from row in map.Elements("row")
select (int.Parse(row.Value))).ToArray()).ToArray()));
But I know I need a string split on commas in here somewhere; and anyway I get a “select not found” error on level.Element(“map”).
My finished array should look like {{0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0},{2,-1,-1,-1,-1,-1,-1,-1,2,-1,-1,-1,-1,-1,-1,-1,-1,-1,2},…
Can anyone help?
–EDIT–
I now have:
var test = (from level in xDoc.Descendants("level")
select (from row in level.Elements("map").Elements("row")
select (from col in row.Value.Split(',')
select (int.Parse(col))).ToArray()).ToArray());
which is giving me the data I want, but the “test” output is showing up in the debug as being of type: {System.Linq.Enumerable.WhereSelectEnumerableIterator<System.Xml.Linq.XElement,int[][]>}
Getting closer, but still not quite right!
Just make it
And then it might just make sense to call it
colinstead ofrow.Why convert? A jagged array seems fine.
Edit
I gave it a test :
Which gives you an
IEnumerabl<IEnumerable<int[][]>>(level and map lists).So
test.First().First()is your first array.