C# 反序列化列表<;ArrayList>;对象

C# 反序列化列表<;ArrayList>;对象,c#,xml,deserialization,C#,Xml,Deserialization,我正试图将XML反序列化为对象,但有一种情况让我陷入困境。谁能帮帮我吗 XML: 代码: [XmlRoot(“级别”)] 公共类LData { [XmlArray(“扭曲块”)] [XmlArrayItem(“Warp_Block”,typeof(WarpBlock),IsNullable=false] 公共列表块; } 公共类LBlock { [XmlAttribute(“行”)] 公共int row; [xmldattribute(“col”)] 公共int col; } 公共类块 {

我正试图将XML反序列化为对象,但有一种情况让我陷入困境。谁能帮帮我吗

XML:


代码:

[XmlRoot(“级别”)]
公共类LData
{
[XmlArray(“扭曲块”)]
[XmlArrayItem(“Warp_Block”,typeof(WarpBlock),IsNullable=false]
公共列表块;
}
公共类LBlock
{
[XmlAttribute(“行”)]
公共int row;
[xmldattribute(“col”)]
公共int col;
}
公共类块
{
[XmlArray(“Warp_块”)]
[XmlArrayItem(“块”,typeof(LBlock),IsNullable=false]
公共列表块;
公共数据块()
{
WarpBlocks=新列表();
}
}

我能够反序列化到一个级别,即我得到一个项目对象列表,但单个项目对象不包含块对象列表。我做错了什么

我不知道您在这里到底在做什么,但反序列化(至少是二进制反序列化。我不知道,但我怀疑XML序列化也是如此)。在反序列化
列表
字典
时,列表将填充
null
值(如果是值类型,则为默认值),直到反序列化构造函数退出。只有在退出构造函数后,列表中才会填充实际的反序列化
T
s

这意味着,如果要对列表执行某些操作,则不能在构造函数中执行。相反,您可以创建一个方法,该方法包含需要对列表执行的任何工作,并且只要使用
[OnDeserializedAttribute]
属性对其进行注释,则在列表填充后调用该方法,但在反序列化返回之前调用该方法。该方法可以有任何名称


有关详细信息,请参阅。

将您的LData类更改为:

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

类项目
-要么这是您的问题,要么我们没有看到真正的代码。您如何反序列化?如果您使用的是
XmlSerializer
,则可能需要将类公开。@Henk-无法在此处发布原始代码。因此,我们必须制作此示例。。但我希望你能得到我想要做的。@Nekresh-是的。所有的类都是公共的。@Sharath:我更喜欢原始类的复制粘贴,可能需要一些删除。这很有可能会让我们追查你的打字错误,而不是问题。我试过这个。。对于WarpBlock类,但在output@Yogesh-试过这个。。因此,我可以将第一个元素读入列表,但不能读入第二个元素……您所说的第二个元素是哪一个?在上面的XML中,Warp_块包含两个Warp_块对象,但它当前仅读取第一个对象。@Yogesh-我已仔细检查了代码,它们是相似的。唯一的区别是我使用XmlTextReader从文件中读取XML,然后将其反序列化。@Yogesh-我的道歉。。这是可行的,但不是我想要的方式。输出实际上是包含所有4个块项的WarpBlock对象。但实际上,我想要的是每两个WarpBlock对象中包含两个块。
  [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>();
      }
   }
[XmlRoot("Level")]
public class LData
{
    [XmlElement("Warp_Blocks")]
    public List<WarpBlock> WarpBlocks;
}
[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();
    }
}
7, 7
2, 7
4, 4
3, 7