将 XML 反序列化为 C# 二维列表

Deserialize XML to C# 2D List

我正在尝试将我的 XML 文档转换为 C# 2D 列表。但是当我这样做时,列表是空的...

这是我的 XML 文件:

<?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>

这是我的 类:

[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; }
}

这是我的解串器代码:

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

问题是,当我尝试检查每个列表的长度时,我的第一个列表的长度是 1,这是正常的,但第二个是 = 0... 我想说的是我想得到这样的列表: List < level > levels 并且在每个级别都是一个 List < theItem > theItems with theItem 元素和每个 theItem 都有它的内容,就像在 XML 文件中一样...... 我尝试了多种方法,但没有找到解决问题的方法。 提前致谢,抱歉我的英语不好!

我相信这可能有效。像这样更改你的级别 class:

[Serializable]
public class level
{
    [XmlAttribute("ID")]
    public string ID { get; set; }
    [XmlArray("level")]
    public List<theItem> items { get; set; }
}

然后像这样更改 xml:

<?xml version="1.0" encoding="utf-8"?>
<data>
  <levels>
    <level ID="0">
      <items>
      <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>
    </items>
    </level>
  </levels>
</data>

编辑:

正如 Ondrej 正确指出的那样:您的大部分位置值不能序列化为整数。所以把它们改成浮点数会更好:

[XmlElement("positionX")]
public float positionX { get; set; }
[XmlElement("positionY")]
public float positionY { get; set; }

编辑 2:更简单的解决方案

在您的原始 XML 文件中您使用 <level> 作为 <theItem> 的列表,因此您必须更新对象以反映这一点。您可以简单地通过使 level 像这样扩展一个 List 并使用原始的 XML 结构来做到这一点:

[Serializable]
public class level : List<theItem>
{
    [XmlAttribute("ID")]
    public string ID { get; set; }
}