2017-01-23 66 views
0

列表我想序列名單列表,但我得到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; 
     } 
    } 
} 

我的全是例外This/ 沒有例外的非系統類型'System.InvalidOperationException'的最終產品dans System.Xml.dll

Informationssupplémentaires:Une erreur s'est produite lors de laréflexiondu type'GestionFacturationCore.ListDevis'。

+0

我有類似的東西 - 這是它是如何完成http://stackoverflow.com/a/40165240/1704458 –

+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); 
    } 
} 

列表變量是類型的BindingList <德維斯>但您將其序列化爲ListDevis - 更改XmlSerializer構造函數invocatio N到:

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

還需要一個參數的構造函數添加到COMMANDE類。

+0

我做到了這一點,但最終我有相同的System.InvalidOperationException如果我標記公開列表 List {get;組; }作爲內部它將編譯 –

+0

所以你可以發佈你的代碼和你有完整的例外 – Damian