【问题标题】:Xml Serialization outputting an empty object when attempting to serialize a list of objectsXml 序列化在尝试序列化对象列表时输出空对象
【发布时间】:2020-07-19 01:20:17
【问题描述】:

我正在尝试序列化配置列表。每个配置都包含三种对象类型,我已将它们全部设为可序列化。出于某种原因,当我尝试序列化列表时它只输出:

- <cart_configurations>
    <ArrayOfAnyType/>
</cart_configurations>

我假设此问题与尝试序列化 List 而不是数组有关?

这是我的保存/加载代码

public static class SaveSystem
{
    public static void UpdateConfigurations(Configurations configurations)
    { 
        string path = Application.persistentDataPath + "/CartConfigurations.xml";

        XmlSerializer serializer = new XmlSerializer(typeof(Configurations));
        FileStream stream = new FileStream(path, FileMode.Create);

        serializer.Serialize(stream, configurations);
        stream.Close();
    }
    public static Configurations LoadConfigurations()
    {
        string path = Application.persistentDataPath + "/CartConfigurations.xml";

        if (File.Exists(path))
        {

            XmlSerializer serializer = new XmlSerializer(typeof(Configurations));


            FileStream stream = new FileStream(path, FileMode.Open);

            Configurations configurations = (Configurations) serializer.Deserialize(stream);
            stream.Close();

            return configurations;
        }
        else
        {
            Debug.Log("nothin");
            return null;
        }
    }
}

这是我尝试保存/加载的对象的代码

[System.Serializable]
[System.Xml.Serialization.XmlRoot("cart_configurations")]
public class Configurations : List<Configuration>
{
    [XmlElement("configuration")]
    public List<Configuration> cart_configurations = new List<Configuration>();
}
[System.Serializable]
public class Configuration : List<object>
{
    [XmlElement("name")]
    public string Name;

    [XmlElement("option_data")]
    public Dropdown.OptionData optionData = new Dropdown.OptionData();

    [XmlElement("tire_type")]
    public TireType TireType;

    [XmlElement("body_type")]
    public BodyType BodyType;

    [XmlElement("spoiler_type")]
    public SpoilerType SpoilerType;

    public Configuration()
    {

    }
    public Configuration(string name,  TireType tireType, BodyType bodyType, SpoilerType spoilerType)
    {
        Name = name;
        optionData.text = name;
        TireType = tireType;
        BodyType = bodyType;
        SpoilerType = spoilerType;
    }
}
[System.Serializable]
public class TireType : object
{
    [XmlElement("type")]
    public string type = "TireType";

    [XmlElement("name")]
    public string Name { get; set; }

    [XmlElement("weight")]
    public int Weight { get; set; }

    [XmlElement("grip")]
    public int Grip { get; set; }

    [XmlElement("mesh")]
    public GameObject Mesh { get; set; }

    [XmlElement("collider")]
    public GameObject Collider { get; set; }

    public TireType()
    {

    }
    public TireType(string name, int weight, int grip, GameObject mesh, GameObject collider)
    {
        Name = name;
        Weight = weight;
        Grip = grip;
        Mesh = mesh;
        Collider = collider;
    }


}

BodyType 和 Spoiler Type 与 TireType 类几乎相同。 此外,Configuration 类仅实现 List,因为 XmlSerializer 需要添加功能。如果您知道更好的解决方案,也将不胜感激。

非常感谢您提前提供的帮助。

【问题讨论】:

  • Xml 序列化不允许在根处使用数组。您有: public class Configurations : List 这是无效的。删除继承的类。

标签: c# xml unity3d xml-serialization xmlserializer


【解决方案1】:

见下面的代码。我注释掉了未定义的对象。删除了继承的类并添加了“{get;set}”而不是“new List();”。请参阅下面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            Configurations configuations = new Configurations()
            {
                cart_configurations = new List<Configuration>() {
                    new Configuration() {  Name = "abc", TireType = new TireType()
                        { Grip = 1, Name = "def", type = "round", Weight = 123}
                    },
                    new Configuration() {  Name = "def", TireType = new TireType()
                        { Grip = 2, Name = "ghi", type = "square", Weight = 456}
                    }

                }
            };
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(FILENAME, settings);
            XmlSerializer serializer = new XmlSerializer(typeof(Configurations));
            serializer.Serialize(writer, configuations);
        }
    }
    [System.Serializable]
    [System.Xml.Serialization.XmlRoot("cart_configurations")]
    public class Configurations
    {
        [XmlElement("configuration")]
        public List<Configuration> cart_configurations { get; set; }
    }
    [System.Serializable]
    public class Configuration
    {
        [XmlElement("name")]
        public string Name;

        //[XmlElement("option_data")]
        //public Dropdown.OptionData optionData = new Dropdown.OptionData();

        [XmlElement("tire_type")]
        public TireType TireType;

        //[XmlElement("body_type")]
        //public BodyType BodyType;

        //[XmlElement("spoiler_type")]
        //public SpoilerType SpoilerType;

        public Configuration()
        {

        }
        //public Configuration(string name, TireType tireType, BodyType bodyType, SpoilerType spoilerType)
        //{
        //    Name = name;
        //    optionData.text = name;
        //    TireType = tireType;
        //    BodyType = bodyType;
        //    SpoilerType = spoilerType;
        //}
    }
    [System.Serializable]
    public class TireType : object
    {
        [XmlElement("type")]
        public string type = "TireType";

        [XmlElement("name")]
        public string Name { get; set; }

        [XmlElement("weight")]
        public int Weight { get; set; }

        [XmlElement("grip")]
        public int Grip { get; set; }

        //[XmlElement("mesh")]
        //public GameObject Mesh { get; set; }

        //[XmlElement("collider")]
        //public GameObject Collider { get; set; }

        public TireType()
        {

        }
        //public TireType(string name, int weight, int grip, GameObject mesh, GameObject collider)
        //{
        //    Name = name;
        //    Weight = weight;
        //    Grip = grip;
        //    Mesh = mesh;
        //    Collider = collider;
        //}


    }
}

【讨论】:

    猜你喜欢
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    相关资源
    最近更新 更多