c# - XmlException:找不到类型
问题描述
我用代码序列化了一个类:
public void Save()
{
string fichero = Application.persistentDataPath + "/" + nombreJuego + ".dat";
FileStream file = File.Create(fichero);
DataContractSerializer bf = new DataContractSerializer(typeof(JuegoMesa));
MemoryStream streamer = new MemoryStream();
bf.WriteObject(streamer, this);
streamer.Seek(0, SeekOrigin.Begin);
file.Write(streamer.GetBuffer(), 0, streamer.GetBuffer().Length);
file.Close();
}
并将其反序列化:
public void Load()
{
string fichero = Application.persistentDataPath + "/" + nombreJuego + ".dat";
Debug.Log(fichero);
DataContractSerializer bf = new DataContractSerializer(typeof(JuegoMesa));
try
{
JuegoMesa leido = null;
object objeto;
MemoryStream streamer = new MemoryStream(File.ReadAllBytes(fichero));
streamer.Seek(0, SeekOrigin.Begin);
objeto = bf.ReadObject(streamer);
leido = (JuegoMesa)objeto;
ActualizarListas(leido.listaListas);
ActualizarPropiedades(leido.listaPropiedades);
ActualizarRecursos(leido.listaRecursos);
ActualizarComponentes(leido.listaComponentes);
}
catch (FileNotFoundException)
{
listaListas.Clear();
listaPropiedades.Clear();
listaRecursos.Limpiar();
listaComponentes.Clear();
Save();
}
}
在阅读对象时给了我一个例外:
XmlException:找不到类型;名称:PropiedadTexto,命名空间:http: //schemas.datacontract.org/2004/07/ System.Runtime.Serialization.XmlFormatterDeserializer.GetTypeFromNamePair(System.String 名称,System.String ns)
该类具有以下要序列化的元素:
public string nombreJuego;
public List<TextosPredefinidos> listaListas;
public List<Propiedad> listaPropiedades;
public ListaRecursos listaRecursos;
public List<ListaRecursos> listaComponentes;
这
List<Propiedad>
是从 Propiedad 类派生的类的对象列表。例如有错误的类
[Serializable]
public class PropiedadTexto : Propiedad
{
public string textoDescriptivo;
public PropiedadTexto() : base()
{
}
...
}
有谁知道可能是什么问题?
为我糟糕的英语道歉。
谢谢。
解决方案
在尝试创建一个最小/完整/可验证的示例时,我找到了解决方案。我仍然不明白为什么会出现错误,但解决方案是创建一个派生自 DataContractResolver 的类并通知未知类型
class ResolverXml : DataContractResolver
{
private XmlDictionary dictionary = new XmlDictionary();
public ResolverXml()
{
}
public override bool TryResolveType(Type dataContractType, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
{
if (dataContractType == typeof(PropiedadTexto))
{
XmlDictionary dictionary = new XmlDictionary();
typeName = dictionary.Add("PropiedadTexto");
typeNamespace = dictionary.Add("JuegoMesa");
return true;
}
else
{
return knownTypeResolver.TryResolveType(dataContractType, declaredType, null, out typeName, out typeNamespace);
}
}
// public override Type ResolveName(string typeName, string typeNamespace, DataContractResolver knownTypeResolver)
public override Type ResolveName(string typeName, string typeNamespace, Type type, DataContractResolver knownTypeResolver)
{
if (typeName == "PropiedadTexto" && typeNamespace == "JuegoMesa")
{
return typeof(PropiedadTexto);
}
else
{
return knownTypeResolver.ResolveName(typeName, typeNamespace, type, null);
}
}
}
并且必须在 DataContractSerializer 构造函数中指明
DataContractSerializer bf = new DataContractSerializer(typeof(PruebaXml), null, Int32.MaxValue, false, false, null, new ResolverXml());
但是统一不识别 DataContractResolver 类。使用的替代方法是通知 DataContractSerializer 构造函数中的未知类型。
private List<Type> tiposConocidos;
tiposConocidos.Add(typeof(PropiedadTexto));
tiposConocidos.Add(typeof(PropiedadEntero));
DataContractSerializer bf = new DataContractSerializer(typeof(JuegoMesa), tiposConocidos);
推荐阅读
- python - 如何在mako python中编写url
- javascript - VueJS/Vuetify - @change set loading="true" 输入
- android - android应用程序来跟踪两个用户之间的距离
- python - pandas DataFrame 列未显示在 df.columns
- python - 本地服务器不工作。错误连接被拒绝。python 3烧瓶ubuntu 16
- node.js - nodejs工作线程中的ERR_WORKER_OUT_OF_MEMORY
- javascript - Angular Material,为什么动画不起作用?
- sql - 使用分组功能时将表与自身连接起来
- sas - ODS Excel/Proc 报告 - 没有自动换行/合并单元格?
- python - 将 python 脚本中的数据通过管道传输到 .csv 或 .txt 文件中