XML : Deserializing part of XML to a List

I have the following XML data:

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  <data-set xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">      <record>          <LevelNum>0</LevelNum>          <LevelName>Level 0</LevelName>          <MaxBubbles>40</MaxBubbles>          <MaxVisibleBubbles>30</MaxVisibleBubbles>          <StartingPointValue>11</StartingPointValue>          <MaxLevelTime>78</MaxLevelTime>          <LevelPassScore>3000</LevelPassScore>          <TapsToPopStandard>1</TapsToPopStandard>          <InitialVisibleBubbles>9</InitialVisibleBubbles>          <LevelDescription>Level 80</LevelDescription>          <SeqLinear>1</SeqLinear>          <SeqEven>0</SeqEven>          <SeqOdd>0</SeqOdd>          <SeqTriangular>0</SeqTriangular>          <SeqSquare>0</SeqSquare>          <SeqLazy>0</SeqLazy>          <SeqFibonacci>0</SeqFibonacci>          <SeqPrime>0</SeqPrime>          <SeqDouble>0</SeqDouble>          <SeqTriple>0</SeqTriple>          <SeqPi>0</SeqPi>          <SeqRecaman>0</SeqRecaman>      </record>  </data-set>    

I am currently reading this data with the following:

      //---------------------------------------------------------------------------------------------------------      // ReadLevels      //---------------------------------------------------------------------------------------------------------      // Reads the contents of the levelInfo.xml file and builds a list of level objects with the data in the       // xml file.  Allows for easy changing of level setup and addint new levels      //---------------------------------------------------------------------------------------------------------      public List<Level> ReadLevels(XDocument levelInfo)      {          levelInfo = XDocument.Load ("./levelinfo.xml");            List<Level> lvl = (from level in levelInfo.Root.Descendants ("record") // "record" has to match the record level identifier in the xml file              select new Level {                  LevelNum = int.Parse (level.Element ("LevelNum").Value),                  LevelName = level.Element ("LevelName").Value,                  MaxBubbles = int.Parse (level.Element ("MaxBubbles").Value),                  MaxVisibleBubbles = int.Parse (level.Element ("MaxVisibleBubbles").Value),                  StartingPointValue = int.Parse (level.Element ("StartingPointValue").Value),                  MaxLevelTime = int.Parse (level.Element ("MaxLevelTime").Value),                  LevelPassScore = int.Parse (level.Element ("LevelPassScore").Value),                  TapsToPopStandard = int.Parse (level.Element ("TapsToPopStandard").Value),                  InitialVisibleBubbles = int.Parse (level.Element ("InitialVisibleBubbles").Value),                  LevelDescription = level.Element ("LevelDescription").Value,                  SeqLinear = (bool)level.Element ("SeqLinear"),                  SeqEven = (bool)level.Element ("SeqEven"),                  SeqOdd = (bool)level.Element ("SeqOdd"),                  SeqTriangular = (bool)level.Element ("SeqTriangular"),                  SeqSquare = (bool)level.Element ("SeqSquare"),                  SeqLazy = (bool)level.Element ("SeqLazy"),                  SeqFibonacci = (bool)level.Element ("SeqFibonacci"),                  SeqPrime = (bool)level.Element ("SeqPrime"),                  SeqDouble = (bool)level.Element ("SeqDouble"),                  SeqTriple = (bool)level.Element ("SeqTriple"),                  SeqPi = (bool)level.Element ("SeqPi"),                  SeqRecaman = (bool)level.Element ("SeqRecaman")              }).ToList ();            return lvl;      }    

My current data structure for the level data looks like this:

  public class Level  {      public int LevelNum { get; set; }      public string LevelName { get; set; }      public int MaxBubbles { get; set; }      public int MaxVisibleBubbles { get; set; }      public int StartingPointValue { get; set; }      public int MaxLevelTime { get; set; }      public int LevelPassScore { get; set; }      public int TapsToPopStandard { get; set; }      public int InitialVisibleBubbles { get; set; }      public string LevelDescription { get; set; }      public bool SeqLinear { get; set; }      public bool SeqEven { get; set; }      public bool SeqOdd { get; set; }      public bool SeqTriangular { get; set; }      public bool SeqSquare { get; set; }      public bool SeqLazy { get; set; }      public bool SeqFibonacci { get; set; }      public bool SeqPrime { get; set; }      public bool SeqDouble { get; set; }      public bool SeqTriple { get; set; }      public bool SeqPi { get; set; }      public bool SeqRecaman { get; set; }        public Level ()      {        }  }    

I'm thinking that there is a better way to handle the list of boolean flag properties. Rather than have them all listed and set each one individually, it would probably be better to store these in a list or dictionary.

My problem is I don't know how I would deserialize the XML data to do that. I'm already creating a list of level objects, but I don't know how I would create the 'list within a list' the right way.

Is is possible to create another list with the list I'm already creating using nested linq statements or is there another way I should be doing this?

No comments:

Post a Comment