i have 2 derived classes serialized xml.
while code works fine (xmlserializer, nothing strange), serialization of datascenario causes mydata property items produce xmlelement names base class name:
<datascenario> <mydata> <scenariodata/> <scenariodata/> <scenariodata/> </mydata> <datascenario>
instead, i'm trying have these items produce xmlelement names derived classes
<datascenario> <mydata> <type1/> <type1/> <type2/> </mydata> <datascenario>
is possible? keep in mind need deserialize well; i'm unsure whether deserialize process understand derived objects need created.
sample code i'm using follows.
[serializable] [xmlinclude(typeof(type1))] [xmlinclude(typeof(type2))] public class scenario { [xmlelement("location")] public string location { get; set; } [xmlelement("value")] public string value { get; set; } public scenario() { } } [serializable] [xmltype("type1")] public class type1 : scenario { public fillpointdata() : base() { } } [serializable] [xmltype("type2")] public class type2 : scenario { public testdata() : base() { } } //hosting class of scenarios public datascenario() { public list<scenario> mydata{ get; set; } }
you can define kind of elements in collection xmlarrayitem attribute. if type known (defined did xmlinclude attribute) create tags "type1", "type2". if types not known, still create tag called scenariodata attribute xsi:type="type1" used map type while deserialization.
[xmlarrayitem(typeof(type1))] [xmlarrayitem(typeof(type2))] public list<scenario> children { // getter & setter }
Comments
Post a Comment