Delete object <ArrayList>

I am trying to de-serialize the XML for an object, but I am stuck in one situation. Can anybody help me here.

XML:

<?xml version="1.0" ?>
<Level>
  <Warp_Blocks>
        <Warp_Block>
            <Block row="7" col="7" />
            <Block row="2" col="7" />
        </Warp_Block>
        <Warp_Block>
            <Block row="4" col="4" />
            <Block row="3" col="7" />
        </Warp_Block>
  </Warp_Blocks>
</Level>

the code:

  [XmlRoot("Level")]
   public class LData
    {
        [XmlArray("Warp_Blocks")]
        [XmlArrayItem("Warp_Block",typeof(WarpBlock),IsNullable = false)]
        public List<WarpBlock> WarpBlocks;
    }
   public class LBlock
   {
      [XmlAttribute("row")]
      public int row;
      [XmlAttribute("col")]
      public int col;
   }
   public class WarpBlock
   {
      [XmlArray("Warp_Block")]
      [XmlArrayItem("Block",typeof(LBlock),IsNullable= false)]
      public List<LBlock> WarpBlocks;

      public WarpBlock()
      {
            WarpBlocks = new List<LBlock>();
      }
   }

I can de-serialize one level, that is, I get objects from the list of items, but individual Item objects do not contain List of Block objects. What am I doing wrong here?

+3
source share
2 answers

Modify your LData class as follows:

[XmlRoot("Level")]
public class LData
{
    [XmlElement("Warp_Blocks")]
    public List<WarpBlock> WarpBlocks;
}

EDIT:
I don't know why he is not reading your second Warp_Block. The only possible reason I think it might be that you are doing something else than what you posted in the question. Here is a complete example:

[XmlRoot("Level")]
public class LData
{
    [XmlElement("Warp_Blocks")]
    public List<WarpBlock> WarpBlocks;
}
public class LBlock
{
    [XmlAttribute("row")]
    public int row;
    [XmlAttribute("col")]
    public int col;
}
public class WarpBlock
{
    [XmlArray("Warp_Block")]
    [XmlArrayItem("Block", typeof(LBlock), IsNullable = false)]
    public List<LBlock> WarpBlocks;

    public WarpBlock()
    {
        WarpBlocks = new List<LBlock>();
    }
}
public class Program
{
    public static void Main()
    {
        string test =
            "<?xml version=\"1.0\" ?>" +
            "<Level>" +
            "  <Warp_Blocks>" +
            "        <Warp_Block>" +
            "            <Block row=\"7\" col=\"7\" />" +
            "            <Block row=\"2\" col=\"7\" />" +
            "        </Warp_Block>" +
            "        <Warp_Block>" +
            "            <Block row=\"4\" col=\"4\" />" +
            "            <Block row=\"3\" col=\"7\" />" +
            "        </Warp_Block>" +
            "  </Warp_Blocks>" +
            "</Level>";

        byte[] byteArray = Encoding.ASCII.GetBytes(test);
        MemoryStream stream = new MemoryStream(byteArray);

        XmlSerializer s = new XmlSerializer(typeof (LData));
        LData data = (LData) s.Deserialize(stream);

        foreach (var a in data.WarpBlocks)
            foreach (var b in a.WarpBlocks)
                Console.WriteLine(b.row + ", " + b.col);

        Console.ReadKey();
    }
}

It correctly outputs this:

7, 7
2, 7
4, 4
3, 7
+3
source

, , gotcha ( , . , , XML). List<T> Dictionary<S,T> null ( , ), . , , T s.

, - , . , , , , [OnDeserializedAttribute], , . .

. MSDN.

0

Source: https://habr.com/ru/post/1792516/


All Articles