c# - Deserialize a list having extra properties with XmlSerializer -



c# - Deserialize a list having extra properties with XmlSerializer -

i want deserialize next xml...

<mytype> <items> <itemsum> <value>3</value> </itemsum> <item> <value>1</value> </item> <item> <value>2</value> </item> </items> </mytype>

...into type of next structure...

[xmlroot("mytype")] public class mytype { [xmlarray("items")] [xmlarrayitem("item")] public citems items { get; set; } public class citems : list<citem> { [xmlelement("itemsum")] public citem itemsum { get; set; } } public class citem { [xmlelement("value")] public int value { get; set; } } }

however, if seek using c#'s xmlserializer, itemsum property null. ideas doing wrong?

here is:

public class mytype { [xmlarray("items")] [xmlarrayitem("itemsum", typeof(itemsum))] [xmlarrayitem("item", typeof(simpleitem))] public citems items { get; set; } public class citems : list<item> {} public class itemsum : item {} public class simpleitem : item {} public class item { public int value { get; set; } } }

this way itemsum element of list , can know checking type.

edit: create utilize of computed properties:

public class citems : list<item> { [xmlignore] public itemsum itemsum { { homecoming this.oftype<itemsum>().single(); } } [xmlignore] public ienumerable<simpleitem> simpleitems { { homecoming this.oftype<simpleitem>(); } } }

c# xml-serialization

Comments

Popular posts from this blog

javascript - mongodb won't find my schema method in nested container -

Hibernate criteria by a list of natural ids -

ios - Lagging ScrollView with UIWebview inside -