我有一个我正在使用的XML文档,我正在尝试将其反序列化为一个类。以下是我一直在使用的课程:
[Serializable()]
[XmlRoot("list")]
public class ItemRoot
{
public ItemRoot()
{
Items = new List<Item>();
}
[XmlElement("itemDefinition")]
public List<Item> Items { get; set; }
}
[Serializable()]
public class Item
{
[System.Xml.Serialization.XmlElement("id")]
public int Id { get; set; }
[System.Xml.Serialization.XmlElement("name")]
public string Name { get; set; }
[System.Xml.Serialization.XmlElement("examine")]
public string Examine { get; set; }
[System.Xml.Serialization.XmlElement("equipmentType")]
public string EquipmentType { get; set; }
[System.Xml.Serialization.XmlElement("noted")]
public bool Noted { get; set; }
[System.Xml.Serialization.XmlElement("noteable")]
public bool Noteable { get; set; }
[System.Xml.Serialization.XmlElement("stackable")]
public bool Stackable { get; set; }
[System.Xml.Serialization.XmlElement("parentId")]
public int ParentId { get; set; }
[System.Xml.Serialization.XmlElement("notedId")]
public int NotedId { get; set; }
[System.Xml.Serialization.XmlElement("members")]
public bool Members { get; set; }
[System.Xml.Serialization.XmlElement("specialStorePrice")]
public int SpecialStorePrice { get; set; }
[System.Xml.Serialization.XmlElement("generalStorePrice")]
public int GeneralStorePrice { get; set; }
[System.Xml.Serialization.XmlElement("highAlcValue")]
public int HighAlcValue { get; set; }
[System.Xml.Serialization.XmlElement("lowAlcValue")]
public int LowAlcValue { get; set; }
[System.Xml.Serialization.XmlElement("bonus")]
public List<int> Bonus { get; set; }
public override string ToString()
{
return $"[{Id}]{Name}";
}
}
这里是XML文件的简短摘录,包含数组中的一个项目(我使用的真实文件有多个,只是为了清楚):
<list>
<itemDefinition>
<id>0</id>
<name>Stack Overflow</name>
<examine>Add coffee.</examine>
<equipmentType>NONE</equipmentType>
<noted>false</noted>
<noteable>false</noteable>
<stackable>false</stackable>
<parentId>-1</parentId>
<notedId>-1</notedId>
<members>true</members>
<specialStorePrice>0</specialStorePrice>
<generalStorePrice>0</generalStorePrice>
<highAlcValue>0</highAlcValue>
<lowAlcValue>0</lowAlcValue>
<bonus>
<int>0</int>
<int>0</int>
<int>0</int>
<int>0</int>
<int>0</int>
<int>0</int>
<int>0</int>
<int>0</int>
<int>0</int>
<int>0</int>
<int>0</int>
<int>0</int>
</bonus>
</itemDefinition>
这是我的反序列化方法:
XmlSerializer reader = new XmlSerializer(typeof(ItemRoot));
_file.Position = 0;
object file = reader.Deserialize(_file);
Root = (ItemRoot)file;
_file.Close();
我试过调查similar个问题,但是使用这些解决方案没有产生任何结果。
答案 0 :(得分:0)
您需要在list元素下添加Items节点,然后将项目节点放在它下面。如果您无法更改xml,请尝试直接序列化为数组或列表,而不是作为包含列表的对象。
答案 1 :(得分:0)
如果我理解正确,您的XML包含多个 itemDefinition 部分,并且您希望将其反序列化为 itemDefinition 的列表(s )......
试试这个
Usings .....
class
类.....
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
代码.....
[XmlRoot(ElementName = "bonus")]
public class Bonus
{
[XmlElement(ElementName = "int")]
public List<string> Int { get; set; }
}
[XmlRoot(ElementName = "itemDefinition")]
public class ItemDefinition
{
[XmlElement(ElementName = "id")]
public string Id { get; set; }
[XmlElement(ElementName = "name")]
public string Name { get; set; }
[XmlElement(ElementName = "examine")]
public string Examine { get; set; }
[XmlElement(ElementName = "equipmentType")]
public string EquipmentType { get; set; }
[XmlElement(ElementName = "noted")]
public string Noted { get; set; }
[XmlElement(ElementName = "noteable")]
public string Noteable { get; set; }
[XmlElement(ElementName = "stackable")]
public string Stackable { get; set; }
[XmlElement(ElementName = "parentId")]
public string ParentId { get; set; }
[XmlElement(ElementName = "notedId")]
public string NotedId { get; set; }
[XmlElement(ElementName = "members")]
public string Members { get; set; }
[XmlElement(ElementName = "specialStorePrice")]
public string SpecialStorePrice { get; set; }
[XmlElement(ElementName = "generalStorePrice")]
public string GeneralStorePrice { get; set; }
[XmlElement(ElementName = "highAlcValue")]
public string HighAlcValue { get; set; }
[XmlElement(ElementName = "lowAlcValue")]
public string LowAlcValue { get; set; }
[XmlElement(ElementName = "bonus")]
public Bonus Bonus { get; set; }
}
[XmlRoot(ElementName = "list")]
public class List
{
[XmlElement(ElementName = "itemDefinition")]
public List<ItemDefinition> ItemDefinition { get; set; }
}
我保存了\从应用程序构建文件夹中名为xml.xml的文件中读取XML .....