XMLSerializer: How come some public properties aren't serialized?

  • Thread starter Thread starter Carmine Moleti
  • Start date Start date
C

Carmine Moleti

Hi to everyone,

I'm playing with XMLSerializer under Compact Framework 2.0 (latest SP
applied).

I have a hierarchy of classes I would like to serialize to XML.
The code I've used follows:

FileStream fs = new
FileStream("prova.xml",FileMode.Create,FileAccess.Write);
Type[] tipi = new Type[]
{typeof(Ordine),typeof(RigaOrdine),typeof(Prodotto)};
XmlSerializer xs = new XmlSerializer(typeof(RiepilogoOrdine),tipi);
xs.Serialize(fs,ro);
fs.Close();


And here are the classes I'd like to serialize:

public class Prodotto {

private string codice;
private decimal prezzo;

public Prodotto() {
}

public Prodotto(String codice, decimal prezzo) {
this.codice = codice;
this.prezzo = prezzo;
}

public string Codice {
get {return codice;}
set {codice = value;}
}

public decimal Prezzo {
get {return prezzo;}
set {prezzo = value;}
}
}

public class RigaOrdine {
private Prodotto prodotto;
private int qta;

public RigaOrdine(){
}

public RigaOrdine(Prodotto prodotto){
this.prodotto = prodotto;
}

public Prodotto Prodotto {
get {return prodotto;}
set {prodotto = value;}
}

public int Qta {
get {return qta;}
set {qta = value;}
}
}

public class Ordine: BindingList<RigaOrdine> {
private int totale;

public Ordine() {
totale = 0;
}

public new void Add(RigaOrdine riga) {
base.Add(riga);
totale += riga.Qta;
}

public int Totale {
get {return totale;}
set {totale = value;}
}
}

public class RiepilogoOrdine: BindingList<Ordine> {

public RiepilogoOrdine() {
}
}

And, last but not least, the serialized stuff I've got:

<?xml version="1.0"?>
<ArrayOfArrayOfRigaOrdine
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ArrayOfRigaOrdine>
<RigaOrdine>
<Prodotto>
<Codice>1</Codice>
<Prezzo>0.1</Prezzo>
</Prodotto>
<Qta>0</Qta>
</RigaOrdine>
<RigaOrdine>
<Prodotto>
<Codice>2</Codice>
<Prezzo>0.2</Prezzo>
</Prodotto>
<Qta>0</Qta>
</RigaOrdine>
<RigaOrdine>
<Prodotto>
<Codice>3</Codice>
<Prezzo>0.3</Prezzo>
</Prodotto>
<Qta>0</Qta>
</RigaOrdine>
<RigaOrdine>
<Prodotto>
<Codice>4</Codice>
<Prezzo>0.4</Prezzo>
</Prodotto>
<Qta>0</Qta>
</RigaOrdine>
</ArrayOfRigaOrdine>
.....

As you can see, under <ArrayOfRigaOrdine> there's information missing
about "Ordine" class i.e.: "Totale" hasn't been serialized.
How come is that possible?

Thanks in advance for your help.
Regards,
Carmine
 
Solved.

BindingList is serialized as an array, and its public properties aren't
serialized.

Thus, in order to serialize everything, we need to wrap the BindingList
within another class which will expose the public properties to
serialize as well as the bindinglist itself.
 
Back
Top