首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >XmlException:类型未找到

XmlException:类型未找到
EN

Stack Overflow用户
提问于 2018-11-05 17:07:10
回答 1查看 84关注 0票数 0

我用以下代码序列化了一个类:

代码语言:javascript
运行
复制
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();
}

并将其反序列化为:

代码语言:javascript
运行
复制
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/ http://schemas.datacontract.org/2004/07/ (System.String名称,System.String ns)

该类具有以下要序列化的元素:

代码语言:javascript
运行
复制
public string nombreJuego;
public List<TextosPredefinidos> listaListas;
public List<Propiedad> listaPropiedades;
public ListaRecursos listaRecursos;
public List<ListaRecursos> listaComponentes;

这个

代码语言:javascript
运行
复制
List<Propiedad>

从Propiedad类派生的类的对象列表。例如,带有错误的类

代码语言:javascript
运行
复制
[Serializable]
public class PropiedadTexto : Propiedad 
{
    public string textoDescriptivo;

    public PropiedadTexto() : base()
    {
    }

  ...
}

有人知道问题出在哪里吗?

为我糟糕的英语道歉。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2018-11-06 10:10:52

在尝试创建一个最小/完整/可验证的示例时,我找到了解决方案。我仍然不明白为什么会出现错误,但是解决方案是创建一个从DataContractResolver派生的类并通知未知类型。

代码语言:javascript
运行
复制
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构造函数中表示。

代码语言:javascript
运行
复制
DataContractSerializer bf = new DataContractSerializer(typeof(PruebaXml), null, Int32.MaxValue, false, false, null, new ResolverXml());

但是统一不承认DataContractResolver类。使用的替代方法是通知DataContractSerializer构造函数中的未知类型。

代码语言:javascript
运行
复制
private List<Type> tiposConocidos;
tiposConocidos.Add(typeof(PropiedadTexto));
tiposConocidos.Add(typeof(PropiedadEntero));
DataContractSerializer bf = new DataContractSerializer(typeof(JuegoMesa), tiposConocidos);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53159000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档