简体   繁体   English

Xml 序列化在尝试序列化对象列表时输出空 object

[英]Xml Serialization outputting an empty object when attempting to serialize a list of objects

I'm attempting to serialize a List of configurations.我正在尝试序列化配置列表。 Each configuration consists of three object types which I have made all serializable.每个配置由三个 object 类型组成,我已将它们全部序列化。 For some reason when I attempt to serialize the List it only outputs:出于某种原因,当我尝试序列化 List 它只输出:

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

I'm assuming this issue is related to attempting to serialize a List rather than an array?我假设这个问题与尝试序列化列表而不是数组有关?

Here is my save / load code这是我的保存/加载代码

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;
        }
    }
}

And here is the code for the objects I'm attempting to save / load这是我试图保存/加载的对象的代码

[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 and Spoiler Type are nearly identical to the TireType class. BodyType 和 Spoiler Type 与 TireType class 几乎相同。 Also, the Configuration class only implements List because XmlSerializer required Add functionality.此外,配置 class 仅实现 List 因为 XmlSerializer 需要添加功能。 If you know a better solution that would also be very appreciated.如果您知道更好的解决方案,我们将不胜感激。

Thank you so much for your help in advance.非常感谢您提前提供的帮助。

See code below.请参阅下面的代码。 I commented out objects that were not defined.我注释掉了未定义的对象。 Removed the inherited classes and added a "{get;set}" instead of a "new List();".删除了继承的类并添加了“{get;set}”而不是“new List();”。 See code below:请参见下面的代码:

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;
        //}


    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM