2

I am trying to do deserialization IN SILVERLIGHT using c#. Before i was using XmlArray and it was working but i want to use List instead because i have done the previous part all using List (so looks odd when i use array just for it).

My xml is like:

<ps>
    <disable_others>
        <disable_other>
            <disable_value>1</disable_value>
            <to_disable>skew</to_disable>
            <to_disable>skew_side</to_disable>
        </disable_other>
        <disable_other>
            <disable_value>0</disable_value>
            <to_disable>automodel</to_disable>
        </disable_other>
    </disable_others>
<ps>

And my trY to deserialize it is:

[XmlRoot(ElementName = "ps")]
    public class ps
    {
      [XmlArray("disable_others")]
      [XmlArrayItem("disable_other", IsNullable = false)]  
      public List<string> Disable_Others { get; set; } 

      /* I know it can be done using the below but i have to use List not array:
              [XmlArray("disable_others")]
        [XmlArrayItem("disable_other", IsNullable = false)]
        public Disable_Other[] Disable_Others { get; set; } */



    }


[XmlRoot(ElementName = "disable_Others")]
    public class Disable_Others
    {
        [XmlElement("disable_other")]
        public List<Disable_Other> Disable_Other { get; set; }

    }

[XmlRoot(ElementName = "Disable_Other")]
    public class Disable_Other
    {
        [XmlElement("disablingitem")]
        public int DisablingItem { get; set; }

        [XmlElement("to_disable")]
        public string[] To_Disable { get; set; }

        [XmlElement("disable_value")]
        public byte Disable_Value { get; set; }

    }

Could some one please help me achieving the target using List instead of array(XmlArray) ?

6
  • If you already have it working for an array, can't you just use Enumerable.ToList() on the array? (Or is that too inefficient?) Commented Jun 17, 2014 at 15:28
  • @MatthewWatson No i have to do it manually. Commented Jun 17, 2014 at 15:29
  • u can use xmlarray...with a list field...and u have to mark your classes as [serializable]. Commented Jun 17, 2014 at 15:45
  • @terrybozzio you mean something like : [XmlArray("disable_others")] [XmlArrayItem("disable_other", IsNullable = false)] public List<Disable_Other> Disable_Others { get; set; } Commented Jun 17, 2014 at 15:48
  • more like: [XmlArray("disable_others")] [XmlArrayItem(typeof(disable_other), IsNullable = false)] public List<Disable_Other> Disable_Others { get; set; }. Commented Jun 17, 2014 at 15:50

1 Answer 1

2

Your XML should be formatted like this:

<?xml version="1.0"?>
<ps xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <disable_others>
    <Disable_Other>
      <disablingitem>0</disablingitem>
      <to_disable>
        <string>skew</string>
        <string>skew_side</string>
      </to_disable>
      <disable_value>1</disable_value>
    </Disable_Other>
    <Disable_Other>
      <disablingitem>0</disablingitem>
      <to_disable>
        <string>automodel</string>
      </to_disable>
      <disable_value>0</disable_value>
    </Disable_Other>
  </disable_others>
</ps>

After this your classes should be the following:

    [Serializable]
    [XmlRoot(ElementName = "ps")]
    public class ps
    {
        [XmlArray("disable_others")]
        [XmlArrayItem(typeof(Disable_Other), IsNullable = false)]
        public List<Disable_Other> Disable_Others { get; set; }

        public ps()
        {
            Disable_Others = new List<Disable_Other>();
        }

    }

    [Serializable]
    [XmlRoot("disable_other")]
    public class Disable_Other
    {
        [XmlElement("disablingitem")]
        public int DisablingItem { get; set; }

        //[XmlElement("to_disable")]
        [XmlArray("to_disable")]
        public string[] To_Disable { get; set; }

        [XmlElement("disable_value")]
        public byte Disable_Value { get; set; }

    }

And in your code to deserialize like this:

ps p;
XmlSerializer serializer = new XmlSerializer(typeof(ps));
using (StreamReader reader = new StreamReader("test1.xml"))
{
    //basic exception handling...
    try
    {
        p = (ps)serializer.Deserialize(reader);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
 }
Sign up to request clarification or add additional context in comments.

3 Comments

sorry i am not able to acces the "skew" and "skew_side". And why i need to have array to access the data inside Disable_Other[index] ? I am trying t do "object1.Disable_Others[0].To_Disable" ans it show 0 and null for everything inside.
And is it must to use [Serializable] because before i was not using it and it was working (when iwas working like this public Disable_Other[] Disable_Others { get; set; }) even without [Serializable].
the way your xml is formatted there are 2 to_disable values,and in your code To_Disable is an array and in xml it should be like in my example,please try it out,make a xml file and paste my example xml,then create the 2 classes as i did and in your calling code place my deserializing example and you will see everything working...

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.