[英]Xml Serialization outputting an empty object when attempting to serialize a list of objects
我正在嘗試序列化配置列表。 每個配置由三個 object 類型組成,我已將它們全部序列化。 出於某種原因,當我嘗試序列化 List 它只輸出:
- <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 class 幾乎相同。 此外,配置 class 僅實現 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;
//}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.