c# - Deserialize array into complex object -


my xml message looks like:

<msg>     <reply userid="sales" requestid="2" index="1" pagesize="1000" total="1" type="order">         <order id="12db8625cd4-000" owner="sales">             <qty size="1" working="0"/>             <price limit="0.0"/>         </order>                     <order id="12db8636344-000" owner="sales">             <qty size="1000" working="0"/>             <price limit="0.0"/>         </order>     </reply> </msg> 

how can define order object read reply array? objects looks like:

[xmlrootattribute("reply")] public class messagereply {     [xmlattribute("userid")]     public string userid { get; set; }      [xmlattribute("requestid")]     public string requestid { get; set; }      [xmlattribute("type")]     public string type { get; set; }      [xmlarrayitem(typeof(order))]     public list<order> orders { get; set; } }  [xmlrootattribute("order")] public class order {     [xmlattribute("id")]     public string id { get; set; }     [xmlattribute("owner")]     public string owner { get; set;}     [xmlattribute("assignee")]     public string assignee { get; set; }     [xmlattribute("instrumentid")]     public string instrumentid { get; set; }     [xmlattribute("side")]     public string side { get; set;}     [xmlattribute("type")]     public string type { get; set; } } 

in case orders should in separate element tag orders. want read them reply element. have idea change @ objects xml attributes?

change:

[xmlarrayitem(typeof(order))] public list<order> orders { get; set; } 

to:

[xmlelement("order")] public list<order> orders { get; set; } 

well... strictly speaking i'd inclined (i don't settable lists) use:

private list<order> orders; [xmlelement("order")] public list<order> orders {get{ return orders ?? (orders = new list<order>());}} 

you need different root object:

[xmlroot("msg")] public class message {     [xmlelement("reply")]     public messagereply reply { get; set; } } 

then works:

var ser = new xmlserializer(typeof(message)); messagereply reply; using(var reader = new stringreader(xml)) {     reply = ((message)ser.deserialize(reader)).reply; } 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -