Xml序列化c#enums仅适用于某些计算机
本文关键字:计算机 适用于 序列化 c#enums Xml | 更新日期: 2023-09-27 17:57:26
我在c#for windows 7 64位中的xml序列化有问题。我想序列化以下类:
[XmlRoot("configuration")]
public class ClaseQueSeSerializa
{
[XmlElement(ElementName = "Nombre")]
public string Nombre { get; set; }
[XmlElement(ElementName = "Edad")]
public int Edad { get; set; }
[XmlElement(ElementName = "tipoDeFichero", Type = typeof (Enumerados.teOrigenDato))]
//[XmlIgnore]
public Enumerados.teOrigenDato EnumeradoOrigen { get; set; }
public ClaseQueSeSerializa()
{
Nombre = "John Connor";
Edad = 15;
EnumeradoOrigen = Enumerados.teOrigenDato.Fichero;
}
}
这就是序列化的方法:
public static class Serializador
{
public static object Deserializar(string file, Type type)
{
try
{
XmlSerializer xmlSerz = new XmlSerializer(type);
using (StreamReader strReader = new StreamReader(file, Encoding.Default, true))
{
object obj = xmlSerz.Deserialize(strReader);
return obj;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
public static object Serializar(string file, Object obj)
{
try
{
XmlSerializer serializer = new XmlSerializer(obj.GetType());
using (StreamWriter stream = new StreamWriter(file, false, Encoding.Default))
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add(String.Empty, String.Empty);
serializer.Serialize(stream, obj, ns);
}
return true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
这是方法调用:
if (File.Exists(RUTA_INSTALACION_CAM + @"'prueba.xml"))
claseQueSeSerializa = (ClaseQueSeSerializa)Serializador.Deserializar(RUTA_INSTALACION_CAM + @"'prueba.xml", typeof(ClaseQueSeSerializa));
else
Serializador.Serializar(RUTA_INSTALACION_CAM + @"'prueba.xml", claseQueSeSerializa);
当我运行时,它会给我以下错误:反映类型NameProject的错误。ErrorSerializarEnumerados然而,当我在其他电脑中运行生成的exe时,它可以工作。
此外,下面的代码序列化了我的类,在我的计算机中没有错误:
[XmlRoot("configuration")]
public class ClaseQueSeSerializa
{
[XmlElement(ElementName = "Nombre")]
public string Nombre { get; set; }
[XmlElement(ElementName = "Edad")]
public int Edad { get; set; }
//[XmlElement(ElementName = "tipoDeFichero", Type = typeof (Enumerados.teOrigenDato))]
[XmlIgnore]
public Enumerados.teOrigenDato EnumeradoOrigen { get; set; }
public ClaseQueSeSerializa()
{
Nombre = "John Connor";
Edad = 15;
EnumeradoOrigen = Enumerados.teOrigenDato.Fichero;
}
}
所以我认为我有一个错误,当串行枚举只有在一些窗口7 64位
所有测试电脑都安装了windows7 64位。
我快要疯了。某个天才知道问题出在哪里?
我解决了将.net框架4.0更新到4.5的问题。
将编码从:Encoding.Default
更改为:Encoding.UTF8