I am using silverlight ot achieve deserialisation of xml which looks like this:
String xmlString=
<attributes>
<value>1</value>
<showstatus>yes</showstatus>
<disableothers>
<disableother>
<disablevalue>1</disablevalue>
<todisable>skew</todisable>
<todisable>skew_side</todisable>
</disableother>
<disableother>
<disablevalue>0</disablevalue>
<todisable>automodel</todisable>
</disableother>
</disableothers>
</attributes>
In my attempt to achieve this i feel like i have something in the classes. The classes are as below:
[XmlRoot(ElementName = "attributes")]
public class Attributes
{
[XmlElement("disableOthers")]
public List<DisableOthers> DisableOthers { get; set; }
}
[XmlRoot(ElementName = "disableOthers")]
public class DisableOthers
{
[XmlElement("disableOthers")]
public List<DisableOther> DisableOther { get; set; }
}
[XmlRoot(ElementName = "disableOther")]
public class DisableOther
{
[XmlElement("disablingitem")]
public int DisablingItem { get; set; }
[XmlElement("todisable")]
public int ToDisable { get; set; }
[XmlElement("disablevalue")]
public int DisableValue { get; set; }
}
Could some one please correct me if my classes are correct corresponding to the given xml ? Would be a big help.
NOTE: The problem exact is when i created object of the parent class then it gives "0" value. And i have already tried it then i came here on stackoverflow.
You don't need DisableOthers
class. Just use property with XmlArrayItem
attribute:
[XmlArrayItem("disableother", IsNullable=false)]
[XmlArray("disableOthers")]
public DisableOther[] DisableOthers { get; set; }
Complete mapping looks like:
[XmlRoot("attributes")]
public class Attributes
{
[XmlElement("value")]
public byte Value { get; set; }
[XmlElement("showstatus")]
public string ShowStatus { get; set; }
[XmlArray("disableothers")]
[XmlArrayItem("disableother", IsNullable = false)]
public DisableOther[] DisableOthers { get; set; }
}
[XmlRoot("disableOther")]
public class DisableOther
{
[XmlElement("disablevalue")]
public byte DisableValue { get; set; }
[XmlElement("todisable")]
public string[] ToDisable { get; set; }
}
Deserialization:
XmlSerializer serializer = new XmlSerializer(typeof(Attributes));
using (var reader = new StringReader(xmlString))
{
var attributes = (Attributes)serializer.Deserialize(reader);
attributes.Dump();
}
Output:
{
Value: 1,
ShowStatus: "yes",
DisableOthers: [
{
DisableValue: 1,
ToDisable: [ "skew", "skew_side" ]
},
{
DisableValue: 0,
ToDisable: [ "automodel" ]
}
]
}