序列化列表<>包括枚举

时间:2013-02-25 18:55:03

标签: c# serialization enums

当我尝试序列化对象列表时,我收到此错误:

  

“反映类型时出错   'System.Collections.Generic.List`1 [Test.MyCustomer]'“

我做错了什么?

public static class Enums
{
    [Serializable]
    public enum MyStatus
    {
        [XmlEnumAttribute("0")]
        Active = 0,
        [XmlEnumAttribute("1")]
        Passive = 1
    }
}

[Serializable]
public class MyCustomer
{
    public string Name { get; set; }
    public Enums.MyStatus Status { get; set; }
}


public static class MySerialize
{
    public static void SerializeToXML(List<MyCustomer> list, string file)
    {
        try
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<MyCustomer>));
            TextWriter textWriter = new StreamWriter(file);
            serializer.Serialize(textWriter, list);
            textWriter.Close();
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }
}

我叫它:

        List<MyCustomer> list = new List<MyCustomer>();
        list.Add(new MyCustomer() { Name = "Carl", Status = Enums.MyStatus.Active });
        list.Add(new MyCustomer() { Name = "Mike", Status = Enums.MyStatus.Passive });

        MySerialize.SerializeToXML(list, @"c:\temp\my.xml");

这一行给出了错误:

            XmlSerializer serializer = new XmlSerializer(typeof(List<MyCustomer>));

如果它有助于堆栈跟踪显示: “在System.Xml.Serialization.XmlReflectionImporter.ImportElement的System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model,String ns,ImportContext context,String dataType,XmlAttributes a,Boolean repeats,Boolean openModel,RecursionLimiter limiter)\ r \ n (TypeModel model,XmlRootAttribute root,String defaultNamespace,RecursionLimiter limiter)\ r \ n位于System.Xml.Serialization.XmlSerializer的System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type,XmlRootAttribute root,String defaultNamespace)\ r \ n。 .ctor(Type type,String defaultNamespace)\ r \ n在System.Xml.Serialization.XmlSerializer..ctor(Type type)\ r \ n,位于SerTest中的Test.MySerialize.SerializeToXML(List`1 list,String file)。 cs:第36行“

0 个答案:

没有答案