【问题标题】:Deserializing XML, where the root object is an array returns only a single element反序列化 XML,其中根对象是一个数组,只返回一个元素
【发布时间】:2016-04-23 22:50:33
【问题描述】:

我有一个正在使用的 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 问题,但使用这些解决方案没有产生任何结果。

【问题讨论】:

  • 我不确定我是否从您的问题中理解了您的问题。 根对象是一个数组只返回一个元素是什么意思?一个有效的 XML 文档必须只有一个根元素,请参阅 en.wikipedia.org/wiki/Root_element。您的 XML 不完整(例如,在 xmlvalidation.com 的验证失败)所以您能否编辑您的问题以给出您的问题的 complete example

标签: c# arrays xml


【解决方案1】:

如果我理解正确,您的 XML 包含多个 itemDefinition 部分,并且您希望将其反序列化为 itemDefinitionList(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.....

【讨论】:

    【解决方案2】:

    您需要在列表元素下方添加一个 Items 节点,然后将 item 节点放在其下方。如果您无法更改 xml,请尝试直接序列化为数组或列表,而不是包含列表的对象。

    【讨论】:

    • 是的,我无法更改 XML。仅使用数组似乎不起作用,序列化结果将&lt;list&gt; 元素更改为&lt;ArrayOfItem&gt; 元素,并将&lt;itemDefitinion&gt; 更改为&lt;Item&gt;,类名。我的列表声明上方有[XmlElement("list")],但我也尝试过[XmlArray("list"), XmlArrayItem("itemDefinition"),但没有产生任何不同的结果。不用说反序列化只是抛出一个异常。
    • 本例中的窍门是把根类完全去掉,如:XmlSerializer reader = new XmlSerializer(typeof(Item[])); _file.Position = 0;目标文件 = reader.Deserialize(_file);根=(项目[])文件; _file.Close();
    猜你喜欢
    • 1970-01-01
    • 2020-07-26
    • 2018-03-14
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 2015-11-11
    • 1970-01-01
    • 2015-08-19
    相关资源
    最近更新 更多