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" ]
}
]
}
/* if you want read objects from xml to c# code
1st create your xml file
2nd your c# code to DeSerializer
*/
//if you want read objects from xml to c# code
//1st create your xml file
<?xml version="1.0" encoding="utf-8" ?>
<FieldConfiguration>
<A>
<B>
<Value>1</Value>
</B>
<B>
<Value>2</Value>
</B>
<ModuleID>1</ModuleID>
</A>
<A>
<B>
<Value>3</Value>
</B>
<B>
<Value>4</Value>
</B>
<ModuleID>2</ModuleID>
</A>
</FieldConfiguration>
//2nd your c# code to DeSerializer
public List<A> FieldCollections;
public SelftestAdv2()
{
XmlSerializer xs = new XmlSerializer(typeof(List<A>), new XmlRootAttribute("FieldConfiguration"));
using (var streamReader = new StreamReader("fff.xml"))
{
FieldCollections = (List<A>)xs.Deserialize(streamReader);
}
}
//if you want opposite, you have objects to save in xml
public SelftestAdv2(int x)
{
B b1 = new B(); b1.v = 3;
B b2 = new B(); b2.v = 4;
B b3 = new B(); b3.v = 5;
B b4 = new B(); b4.v = 6;
A a1 = new A();a1.id = 1;
a1.b.Add(b1);
a1.b.Add(b2);
A a2 = new A();a2.id = 2;
a2.b.Add(b3);
a2.b.Add(b4);
List<A> listA = new List<A>();
listA.Add(a1);
listA.Add(a2);
XmlSerializer xs = new XmlSerializer(typeof(List<A>), new XmlRootAttribute("FieldConfiguration"));
using (var streamReader = new StreamWriter("fff.xml"))
{
xs.Serialize(streamReader,listA);
}
}
`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With