[英]Deserialize XML to C# 2D List
I am trying to convert my XML document to a C# 2D List. 我正在尝试将XML文档转换为C#2D列表。 But when i do this the list is empty... 但是当我这样做时列表是空的...
Here is my XML file: 这是我的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>
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. 问题是,当我尝试检查每个列表的长度时,我的第一个列表的长度为1,这是正常的,但是第二个列表的长度为= 0 ...我想说的是,我想获得一个这样的列表:列出<级别>级别,并在每个级别中列出具有<Item>元素的List <theItem> theItems,使其每个内容都具有XML文件中的内容...我尝试了多种方法,但没有找到解决方案问题。 Thanks in advance and sorry for my bad english! 在此先感谢您,我的英语不好!
I believe this might work. 我相信这可能有效。 Change your level class like this: 像这样更改您的水平班级:
[Serializable]
public class level
{
[XmlAttribute("ID")]
public string ID { get; set; }
[XmlArray("level")]
public List<theItem> items { get; set; }
}
And then change your xml like this: 然后像这样更改您的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>
Edit: 编辑:
As Ondrej correctly points out: Most of your position values cannot be serialized as ints. 正如Ondrej正确指出的那样:大多数位置值不能序列化为int。 So changing these into floats will work better: 因此,将它们更改为float会更好:
[XmlElement("positionX")]
public float positionX { get; set; }
[XmlElement("positionY")]
public float positionY { get; set; }
Edit 2: Simpler solution 编辑2:更简单的解决方案
In your original XML file You're using <level>
as a list of <theItem>
, so you have to update the objects to reflect that. 在原始XML文件中,您将<level>
用作<theItem>
的列表,因此必须更新对象以反映该情况。 You can simply do this by making level extend a List like this and use the original XML structure: 您可以通过以下方式简单地做到这一点:扩展一个这样的List并使用原始XML结构:
[Serializable]
public class level : List<theItem>
{
[XmlAttribute("ID")]
public string ID { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.