4

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.

Sss
  • 1,519
  • 8
  • 37
  • 67

2 Answers2

8

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" ]
    }
  ]
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • 1
    I was just about to write this and yes this is solution for OP problem. – rosko Jun 04 '14 at 08:46
  • Ok let me implement first. will come soon to mark you as answer. Thanks – Sss Jun 04 '14 at 08:47
  • @user234839 yep, sorry. Also you need to use `XmlArray` attribute instead of `XmlElement`. Now it works fine - just checked. I'll add full mapping – Sergey Berezovskiy Jun 04 '14 at 08:56
  • Big thanks. I have adoubt . Why it gives esception when i put "[XmlRoot(ElementName = "disableOther")]" instead of " [XmlRoot("disableOther")]" before public class DisableOther{...} ? – Sss Jun 04 '14 at 09:06
  • 2
    @user234839 there is no difference between these two definitions. Latter one also assigns "disableOther" to ElementName property. Maybe there is some other issue causes exception – Sergey Berezovskiy Jun 04 '14 at 09:08
  • @SergeyBerezovskiy What if i don't have to use arrays ? Just have to use List instead . Do i still dont need to have another Disableothers class ? – Sss Jun 17 '14 at 14:44
1

/* 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);

            }


        }
`