c# - Public fields/properties of a class derived from BindingList<T> wont serialize -


i'm trying serialize class derives bindinglist(floor), floor simple class contains property floor.height

here's simplified version of class

[serializable] [xmlroot(elementname = "custombindinglist")] public class custombindinglist:bindinglist<floor> {     [xmlattribute("publicfield")]     public string publicfield;     private string privatefield;      [xmlattribute("publicproperty")]     public string publicproperty     {         { return privatefield; }         set { privatefield = value; }     } } 

i'll serialize instance of custombindinglist using following code.

xmlserializer ser = new xmlserializer(typeof(custombindinglist)); stringwriter sw = new stringwriter();  custombindinglist clist = new custombindinglist();  floor fl;  fl = new floor(); fl.height = 10; clist.add(fl);  fl = new floor(); fl.height = 10;     clist.add(fl);  fl = new floor(); fl.height = 10; clist.add(fl);  ser.serialize(sw, clist);  string teststring = sw.tostring(); 

yet teststring above ends getting set following xml:

<custombindinglist xmlns:xsi=\"http://www.w3.org/2001/xmlschema-instance\" xmlns:xsd=\"http://www.w3.org/2001/xmlschema\">     <floor height="10" />     <floor height="10" />     <floor height="10" /> </custombindinglist>" 

how "publicfield" or "publicproperty serialize well?

xml serialization handles collections in specific way, , never serializes fields or properties of collection, items.

you either :

  • implement ixmlserializable generate , parse xml (but it's lot of work)
  • wrap bindinglist in class, in declare custom fields (as suggested speps)

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -