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.

3
  • Can you verify it yourself first? Commented Jun 4, 2014 at 8:44
  • You miss value and status in attributes and why don't you try it? Commented Jun 4, 2014 at 8:44
  • What does it gives "0" value mean? And no need to bold stuff... Commented Jun 4, 2014 at 8:56

2 Answers 2

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" ]
    }
  ]
}
Sign up to request clarification or add additional context in comments.

6 Comments

I was just about to write this and yes this is solution for OP problem.
Ok let me implement first. will come soon to mark you as answer. Thanks
@user234839 yep, sorry. Also you need to use XmlArray attribute instead of XmlElement. Now it works fine - just checked. I'll add full mapping
Big thanks. I have adoubt . Why it gives esception when i put "[XmlRoot(ElementName = "disableOther")]" instead of " [XmlRoot("disableOther")]" before public class DisableOther{...} ?
@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
|
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);

            }


        }
`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.