如何序列化列表列表

时间:2017-01-23 20:53:18

标签: c# xml serialization

我想序列化列表列表,但我得到 System.InvalidOperationException

XmlSerializer xmlSer = new XmlSerializer(List.GetType());

这是我的班级

public class ListDevis
{
    public ListDevis()
    {
        list = new BindingList<Devis>();
    }
    private BindingList<Devis> list;

    [XmlArrayItem(ElementName = "Devis", Type = typeof(Devis))]
    public BindingList<Devis> List
    {
        get
        {
            return list;
        }

        set
        {
            list = value;
        }
    } 
    public void ChargerList(string xmlFilePath)
    {
        XmlSerializer xmlSer = new XmlSerializer(List.GetType());
        if (File.Exists(xmlFilePath))
        {
            using (StreamReader streamR = new StreamReader(xmlFilePath))
            {
                try
                {
                    List = (BindingList<Devis>)xmlSer.Deserialize(streamR);
                }
                catch (Exception)
                {

                }

            }
        }
    }

    public void SauvegarderList(string xmlFilePath)
    {
        XmlSerializer xmlSer = new XmlSerializer(typeof(ListDevis));
        using (StreamWriter streamW = new StreamWriter(xmlFilePath))
        {
            xmlSer.Serialize(streamW, List);
        }
    }
}
public class Devis
{
    public Devis()
    {
        commandes = new BindingList<Commande>();
    }

    private string code;
    private string codeClient;
    private DateTime dateDevis;
    private BindingList<Commande> commandes;
    private string statut;

    [DisplayName("Code Devis")]
    public string Code
    {
        get
        {
            return code;
        }

        set
        {
            code = value;
        }
    }
    [DisplayName("Code Client")]
    public string CodeClient
    {
        get
        {
            return codeClient;
        }

        set
        {
            codeClient = value;
        }
    }
    [DisplayName("Date Creation")]
    public DateTime DateDevis
    {
        get
        {
            return dateDevis;
        }

        set
        {
            dateDevis = value;
        }
    }
    [XmlElement(ElementName = "Commandes", Type = typeof(Commande))]
    public BindingList<Commande> Commandes
    {
        get
        {
            return commandes;
        }

        set
        {
            commandes = value;
        }
    }
    [DisplayName("Statut")]
    public string Statut
    {
        get
        {
            return statut;
        }

        set
        {
            statut = value;
        }
    }

}    
public class Commande
{

    private string codeProduit;
    private string libelle;
    private float prixU;
    private int qte;

    public Commande(string codeProduit, string libelle, float prixU, int qte)
    {
        this.CodeProduit = codeProduit;
        this.Libelle = libelle;
        this.PrixU = prixU;
        Qte = qte;
    }
    [XmlElement(DataType = "string", ElementName = "CodeProduit")]
    public string CodeProduit
    {
        get
        {
            return codeProduit;
        }

        set
        {
            codeProduit = value;
        }
    }
    [XmlElement(DataType = "string", ElementName = "Libelle")]
    public string Libelle
    {
        get
        {
            return libelle;
        }

        set
        {
            libelle = value;
        }
    }
    [XmlElement(DataType = "float", ElementName = "Prix")]
    public float PrixU
    {
        get
        {
            return prixU;
        }

        set
        {
            prixU = value;
        }
    }
    [XmlElement(DataType = "int", ElementName = "Qte")]
    public int Qte
    {
        get
        {
            return qte;
        }

        set
        {
            qte = value;
        }
    }
}

我的完全例外是这个/ Une例外非géréedu'System.InvalidOperationException's'est produite dans System.Xml.dll

信息supplémentaires:Une erreur s'est produite lors delaréflexiondutype'GestionFacturationCore.ListDevis'。

1 个答案:

答案 0 :(得分:1)

如果要序列化Class1,则必须将其类型提供给XmlSerializer构造函数

XmlSerializer xmlSer = new XmlSerializer(typeof(Class1));

你有一个方法:

public void SauvegarderList(string xmlFilePath)
{
    XmlSerializer xmlSer = new XmlSerializer(typeof(ListDevis));
    using (StreamWriter streamW = new StreamWriter(xmlFilePath))
    {
        xmlSer.Serialize(streamW, List);
    }
}

List 变量的类型为 BindingList&lt; Devis&gt; 但您将其序列化为 ListDevis - 将XmlSerializer构造函数调用更改为:

XmlSerializer xmlSer = new XmlSerializer(typeof(BindingList<Devis>));

此外,您还需要向Commande类添加无参数构造函数。