我正在尝试序列化配置列表。每个配置都包含三种对象类型,而这些对象类型都已全部序列化。由于某些原因,当我尝试序列化列表时,它仅输出:
- <cart_configurations>
<ArrayOfAnyType/>
</cart_configurations>
我假设这个问题与尝试序列化列表而不是数组有关?
这是我的保存/加载代码
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需要添加功能。如果您知道更好的解决方案,也将不胜感激。
非常感谢您的提前帮助。
请参见下面的代码。我注释掉了未定义的对象。删除继承的类,并添加“ {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;
//}
}
}