1

I'd need an advice. I'm developing some easy game in silverlight and I'd need to load level definitions from XML to List but I'm not sure what's the best way to do it.

my xml looks like

<Levels>
  <Level levelNumber = "1" startingX="2" startingY="2">
    <Cells>
      <Cell CellType="A" PositionX="0" PositionY="0" />
      <Cell CellType="A" PositionX="1" PositionY="0" />
      <Cell CellType="A" PositionX="2" PositionY="0" />
      <Cell CellType="A" PositionX="3" PositionY="0" />
      <Cell CellType="A" PositionX="4" PositionY="0" />
      <Cell CellType="A" PositionX="5" PositionY="0" />
      <Cell CellType="A" PositionX="0" PositionY="1" />
      <Cell CellType="B" PositionX="1" PositionY="1" />
      <Cell CellType="B" PositionX="2" PositionY="1" />
      <Cell CellType="B" PositionX="3" PositionY="1" />
      <Cell CellType="B" PositionX="4" PositionY="1" />
      <Cell CellType="B" PositionX="5" PositionY="1" />
      <Cell CellType="A" PositionX="1" PositionY="2" />
      <Cell CellType="B" PositionX="2" PositionY="2" />
      <Cell CellType="B" PositionX="3" PositionY="2" />
      <Cell CellType="B" PositionX="4" PositionY="2" />
      <Cell CellType="A" PositionX="5" PositionY="2" />
      <Cell CellType="A" PositionX="1" PositionY="3" />
      <Cell CellType="B" PositionX="2" PositionY="3" />
      <Cell CellType="B" PositionX="3" PositionY="3" />
      <Cell CellType="B" PositionX="4" PositionY="3" />
      <Cell CellType="A" PositionX="4" PositionY="3" />
    </Cells>
  </Level>
</Levels>

and i need to load level to list my Level class

public class Level
{
    private int levelNumber;
    private int startingX;
    private int startingY;
    public List<Cell> cellList = new List<Cell>();

    public int LevelNumber
    {
       get { return levelNumber; }
       set { levelNumber = value; }
    }
    ...
}

Could you please give me an advice how to do that?

Gabbo
  • 79
  • 1
  • 2
  • 8

2 Answers2

1

I'd consider using LinqToXml here.

I'd write up a quick demo, but this question demonstrates the concepts you'll need quite well@

De/Serialize directly To/From XML Linq

Edit: For clarity, if you make sure your Cell class is annotated with those Xml attributes, the deserialize behaviour should work correctly for you.

Community
  • 1
  • 1
Russ Clarke
  • 17,511
  • 4
  • 41
  • 45
  • Thanks for reply. My Cell class is annotated as well as the Level class (didn't post the line with annotation).. But could you please write me little dome for deserialization? I can get it working.. – Gabbo Sep 02 '11 at 23:28
  • I'll try and post an example for you when I get back on my PC! – Russ Clarke Sep 03 '11 at 02:29
1

I'd just whip up something quick like this here (assuming there are multiple levels in your xml):

XDocument xdoc = XDocument.Load(url); // assuming you're pulling your xml from a service. 

if (xdoc != null)
{
    var levels =
        (from l in xdoc.Descendants("Level")
            select new Level
            {
                levelNumber = l.Attribute("levelNumber").Value,
                startingX = l.Attribute("startingX").Value,
                startingY = l.Attribute("startingy").Value,
                cellsList = (from c in l.Descendants("Cell")
                            select new Cell
                            {
                                CellType = c.Attribute("CellType").Value,
                                PositionX = c.Attribute("PostionX").Value,
                                PositionY = c.Attribute("PositionY").Value
                            }).ToList()
            }
        ).ToList();
}
arviman
  • 5,087
  • 41
  • 48
  • Should we not also assume that values such as PositionX are integers not strings? – AnthonyWJones Sep 03 '11 at 07:33
  • Many thanks, that attributes are integers but it used Convert.ToInt32(l.Attribute("startingX").Value) and it's completelly working for me.. – Gabbo Sep 03 '11 at 09:24
  • Glad it works. Ideally, you'd also want to dome sort of check too see if the value is not null and if it's compatible with an integer. – arviman Sep 03 '11 at 16:00