Wednesday, 30 December 2015

XML : Deserialize XML to C# 2D List

I am trying to convert my XML document to a C# 2D List. But when i do this the list is empty...

Here is my XML file:

  <?xml version="1.0" encoding="utf-8"?>  <data>    <levels>      <level ID="0">        <theItem ID="0">          <type>Corner</type>          <rotation>180</rotation>          <positionX>-5.5</positionX>          <positionY>-2.5</positionY>        </theItem>        <theItem ID="1">          <type>TripleBar</type>          <rotation>270</rotation>          <positionX>-4.5</positionX>          <positionY>-2.5</positionY>        </theItem>        <theItem ID="2">          <type>Corner</type>          <rotation>270</rotation>          <positionX>-3.5</positionX>          <positionY>-2.5</positionY>        </theItem>        <theItem ID="3">          <type>Bar</type>          <rotation>0</rotation>          <positionX>-5.5</positionX>          <positionY>-1.5</positionY>        </theItem>      </level>    </levels>  </data>    

And this are my classes:

  [Serializable]  public class theItem  {      [XmlAttribute("ID")]      public string ID { get; set; }      [XmlElement("type")]      public string type { get; set; }      [XmlElement("rotation")]      public int rotation { get; set; }      [XmlElement("positionX")]      public int positionX { get; set; }      [XmlElement("positionY")]      public int positionY { get; set; }  }    [Serializable]  public class level  {      [XmlAttribute("ID")]      public string ID { get; set; }      [XmlArray("level")]      public List<theItem> theItems { get; set; }  }    [Serializable]  [XmlRoot("data")]  public class data  {      [XmlArray("levels")]      [XmlArrayItem("level")]      public List<level> levels { get; set; }  }    

And this is my deserializer code:

  var serializer = new XmlSerializer(typeof(data));          using (var reader = XmlReader.Create("LevelData.xml"))          {              data info = (data)serializer.Deserialize(reader);              List<level> levels = info.levels;          }    

The problem is that when i try to check every list's length, my first list is having the length 1 wich is normal, but the second is = 0... What I am trying to say is that I want to get a list like this: List < level > levels and in every level to be a List < theItem > theItems with the theItem elements and every theItem to have its content like in the XML file... I tryed multiple ways but i didn't found solution for my problem. Thanks in advance and sorry for my bad english!

No comments:

Post a Comment