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

Started by
1 comment, last by Andy474 14 years, 9 months ago
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
Advertisement
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>
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

This topic is closed to new replies.

Advertisement