反序列化XML,其中根对象是一个数组,仅返回单个元素

Wallnutkraken

我有一个正在使用的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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

用空数组反序列化XML返回一个带有单个对象的数组

使用JavaScriptSerializer反序列化JSON字符串以返回单个动态对象,而不是键和值的数组

反序列化xml返回零个元素

为什么在序列化/反序列化的情况下仅创建一个父对象

反序列化没有根对象和1个数组的json的ASP.NET MVC

Parcelable with List 仅反序列化第一个元素

使用Gson反序列化另一个对象内的对象数组

如果该对象元素的根标记不存在,如何将XML反序列化为一个对象?

使用动态属性反序列化 JSON,其中包含一个数组到具有泛型类的字典

不反序列化一个对象的实例

Json.net反序列化返回一个空对象

反序列化一个简单的 JSON 数组

C#使用Newtonsoft.json对仅一个属性(找不到根)反序列化Json响应

如何反序列化json,其中数据可以是对象或空数组,并且对象是“字典”格式?

在只有一个条目的情况下,反序列化json,其中属性具有对象而不是数组?

将XML反序列化为简单的单个数组

反序列化Custom对象的序列化ArrayList,添加一个对象,然后重新序列化

将根XML元素反序列化为数组

Golang序列化/反序列化一个空数组,而不是null

反序列化返回的单个或数组数据

Gson-xml 在另一个列表<对象> 中反序列化列表<对象>

使用Jackson将反序列化JSON数组反序列化为单个Java对象

在 Jackson 中反序列化 JSON,其中 key 是一个值

Jackson对嵌套对象进行反序列化,其中一个引用另一个。未解决的ForwardReference

Java List 反序列化并返回一个对象会产生不兼容的类型错误

反序列化xml字符串并忽略root中的第一个元素

反序列化返回空值的 XML 元素

PHP-反序列化为null,但包含一个数组

春天RestTemplate反序列化的XML对象返回NULL