c# - How do I minimize repeated xml attributes when dealing with multiple levels of inheritance? -


i have ui framework xna engine written makes easy define user interfaces via code. looking @ making easier utilize allowing defining of user interfaces via xml.

what i'm stuck on creating deserialization classes. issue root contains collection of items, , items may contain 1 or more child items. right have similar to:

[xmlroot] public class rootclass {     [xmlarray]     [xmlarrayitem("classa", typeof(classa)]     [xmlarrayitem("classb", typeof(classb)]     public list<baseclass> classes { get; set; } }  public class baseclass {     [xmlattribute]     public string name { get; set; }  public class classa : baseclass {     [xmlattribute]     public string avalue { get; set;      [xmlarray]     [xmlarrayitem("classa", typeof(classa)]     [xmlarrayitem("classb", typeof(classb)]     public list<baseclass> children { get; set; } }  public class classb : baseclass {     [xmlattribute]     public string bvalue { get; set;      [xmlelement("classa", typeof(classa)]     [xmlelement("classb", typeof(classb)]     public baseclass singlechild { get; set; } } 

the intention of root can contain number of subclasses, classa elements can contain number of child classes, while classb type may contain single child class.

this has massive maintainability issue because when add new class inherits baseclass (such classc), need add new xmlarrayitem attribute rootclass.classes property and every other list used. if classc contains list of classes, 3 points need maintain xmlarrayitem attribute listings. need make sure single class values have new [xmlelement] attribute added them.

is there easier way keep having repeat these inheritance mappings across every deserialization class?

another approach implement ixmlserializable think. have done in own project handle serialization , deserialization of complex inheritance. can "control" de/serialization readxml/writexml methods overwritten child classes.

implementing ixmlserializable on own more better if have "dynamic" mechanism in framework, extending classes types loaded during runtime of program or if dont want edit attributes every time. if go way, have serialize type of class. , @ deserialization time can create object via "activator" , type have serialized.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -