# [XML, C#] Saving a int[,] to a XML file

This topic is 3279 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys. I am trying to save a multidimentional array of integers (a Tile map) to an XML File, i am currantly having problems saying the acctual array with line breaks. here is the XML file Layout I want when I save
<TileLayer>
<Layout Width="10" Height="10">
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 2 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

</Layout>

</TileLayer>


and here is the code for saving it
XmlElement layoutEle = doc.CreateElement("Layout");

XmlAttribute WidthAtr = doc.CreateAttribute("Width");
WidthAtr.Value = Width.ToString();
XmlAttribute HeightAtr = doc.CreateAttribute("Height");
HeightAtr.Value = Height.ToString();

layoutEle.Attributes.Append(HeightAtr);
layoutEle.Attributes.Append(WidthAtr);

layoutEle.InnerText += "\n";
for(int y = 0; y < Height; y++)
{
string line = string.Empty;

for (int x = 0; x < Width; x++)
{
line += LayerMap[y, x].ToString() + " ";
}
layoutEle.InnerText += (line + "\n");
}


Now this worked When i was saving it into a normal text file, the lines broke after the firs x number of integers and started another map. However all i get here in between the two <layout> tags is a row of numbers. i cannot seem to to get the linebreak in there. Is this problem becuase i cannot save to xml like this? (the numbers have to be in a solid row? or is it a problem with my loop? any help is apprectiated thanks xD

##### Share on other sites
Is the line break really necessary? How do you read the values back from the XML-file?

I believe you can go with a number of choices here. One is that the visual representation of the tile map in the XML file doesn't really matter, as long as the parsing of the values is corrent. For example: the Layout-tag tells me that every row is 10 items wide, so all I need to keep track of is how many numbers you've read so far (every 10th is a new line).
Because when you think of it, the new line is really just there for your own sake, so you can distinguish the view of the map easier. It isn't really necessary for the parser.

Another way to do it is to store every row in a Row-tag, like this:
<TileLayer>  <Layout Width="10" Height="10">    <Row>1 0 0 0 0 0 0 0 0 0</Row>    <Row>1 0 0 0 0 0 0 0 0 0</Row>    <Row>1 0 0 0 0 0 0 0 0 0</Row>    <Row>1 0 0 0 0 0 0 2 0 0</Row>    <Row>1 0 0 0 0 0 0 0 0 0</Row>    <Row>1 0 0 0 0 0 0 0 0 0</Row>    <Row>1 1 1 1 1 1 1 1 1 1</Row>    <Row>0 0 0 0 0 0 0 0 0 0</Row>    <Row>0 0 0 0 0 0 0 0 0 0</Row>    <Row>0 0 0 0 0 0 0 0 0 0</Row>      </Layout>	</TileLayer>

##### Share on other sites
i was following tutorials from ::http://nickgravelyn.com/archive/#tileengine::

the xml is read back in

<Layout Width="10" Height="10">    1 0 0 0 0 0 0 0 0 0     1 0 0 0 0 0 0 0 0 0     1 0 0 0 0 0 0 0 0 0    1 0 0 0 0 0 0 2 0 0    1 0 0 0 0 0 0 0 0 0    1 0 0 0 0 0 0 0 0 0     1 1 1 1 1 1 1 1 1 1    0 0 0 0 0 0 0 0 0 0    0 0 0 0 0 0 0 0 0 0    0 0 0 0 0 0 0 0 0 0      </Layout>	</TileLayer>

in this form (no <row> tags)

so, even if the xml reader is set to read in its wont matter if the file is broken into lines or in one large row of numbers?

Answer : Yes. Thanks for clearing that up

1. 1
Rutin
19
2. 2
3. 3
JoeJ
16
4. 4
5. 5

• 26
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001782
×