【发布时间】:2016-10-05 10:55:16
【问题描述】:
我有一个继承自 List 的类
[Serializable]
public class ListWithVersion<T> : List<T>
{
[XmlElement(ElementName = "version")]
public int version;
public ListWithVersion(IEnumerable<T> collection) : base(collection)
{
}
public ListWithVersion() : base()
{
}
}
我像这样将它序列化为 XML
ListWithVersion<Chapter> lwv = new ListWithVersion<Chapter>();
Chapter chapter = new Chapter();
chapter.dialogs = new List<Dialog>();
lwv.version = 1;
lwv.Add(chapter);
Serialize("lwv.xml", typeof(ListWithVersion<Chapter>), extraTypes, lwv);
private void Serialize(string name, Type type, Type[] extraTypes, object obj)
{
try
{
var serializer = new XmlSerializer(type, extraTypes);
using (var fs = new FileStream(GetPathSave() + name, FileMode.Create, FileAccess.Write))
{
serializer.Serialize(fs, obj);
}
}
catch (XmlException e)
{
Debug.LogError("serialization exception, " + name + " Message: " + e.Message);
}
catch (System.Exception ex)
{
Debug.LogError("exc while ser file '" + name + "': " + ex.Message);
System.Exception exc = ex.InnerException;
int i = 0;
while (exc != null)
{
Debug.LogError("inner " + i + ": " + exc.Message);
i++;
exc = exc.InnerException;
}
}
}
但 XML 文件不包含版本参数。
<?xml version="1.0" encoding="windows-1251"?>
<ArrayOfChapter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Chapter id="0">
<dialogs />
</Chapter>
</ArrayOfChapter>
(version="1.0" 不是我的参数)
我尝试过 XmlAttribute 而不是 XmlElement,也尝试过原始
public int version;
在 XML 中获取版本参数没有任何帮助。
那我该如何解决呢?
【问题讨论】: