Reputation: 1
I want to serialize a list of list but i get System.InvalidOperationException in
XmlSerializer xmlSer = new XmlSerializer(List.GetType());
this is my class
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;
}
}
}
my full exception is this / Une exception non gérée du type 'System.InvalidOperationException' s'est produite dans System.Xml.dll
Informations supplémentaires : Une erreur s'est produite lors de la réflexion du type 'GestionFacturationCore.ListDevis'.
Upvotes: 0
Views: 357
Reputation: 2852
If you want to serialize Class1 you have to provide its type to XmlSerializer constructor
XmlSerializer xmlSer = new XmlSerializer(typeof(Class1));
You have a method:
public void SauvegarderList(string xmlFilePath)
{
XmlSerializer xmlSer = new XmlSerializer(typeof(ListDevis));
using (StreamWriter streamW = new StreamWriter(xmlFilePath))
{
xmlSer.Serialize(streamW, List);
}
}
The List variable is of type BindingList< Devis > but you are serializing it as a ListDevis - change XmlSerializer constructor invocation to:
XmlSerializer xmlSer = new XmlSerializer(typeof(BindingList<Devis>));
Also you need to add a parameterless constructor to the Commande class.
Upvotes: 1