我有一个正在使用的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();
我曾尝试研究类似的问题,但是使用这些解决方案没有产生任何结果。
如果我理解正确的话,你有多个XML itemDefinition节和要反序列化到列表的itemDefinition(S)....
试试这个
正在使用.....
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; }
}
代码.....
static void Main(string[] args)
{
string strXML = File.ReadAllText("xml.xml");
byte[] bufXML = ASCIIEncoding.UTF8.GetBytes(strXML);
MemoryStream ms1 = new MemoryStream(bufXML);
// Deserialize to object
XmlSerializer serializer = new XmlSerializer(typeof(List));
try
{
using (XmlReader reader = new XmlTextReader(ms1))
{
List deserializedXML = (List)serializer.Deserialize(reader);
}// put a break point here and mouse-over deserializedXML….
}
catch (Exception ex)
{
throw;
}
}
我从应用程序构建文件夹中的xml.xml文件中保存\读取了您的XML。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句