0

In the following composition please note the serialization attributes are lowercase and the array property in the root is serialized accordingly but its child element are not honoring this decoration.

I spected this:

<engine>
  <servos>
    <servo>
    </servo>
  </servos>
</engine>

But instead i get this:

<engine>
  <servos>
    <Servo> <!-- here is the problem-->
    </Servo>
  </servos>
</engine>

Code:

    [XmlRoot( "engine" )]
    public class Engine {

    [XmlArray( "servos" )]
    public List<Servo> Servos { 
        get { return servos; } 
        set { servos = value; } 
        }
    }

    [XmlRoot( "servo" )] //Child ignoring lowercase decoration 
    public class Servo {
    }

What is the correct way to serialize as indicated by the attribute?

E-Bat
  • 4,792
  • 1
  • 33
  • 58
  • possible duplicate of [XmlRoot() for Xml Serilization does not work](http://stackoverflow.com/questions/1440845/xmlroot-for-xml-serilization-does-not-work) – toATwork Jun 18 '14 at 14:15

1 Answers1

2

You have to add XmlArrayItem attribute to the Servos property:

 [XmlArrayItem( "servo" )]
 [XmlArray("servos")]
 public List<Servo> Servos { 
     get;
     set ;
     }
 }
Aik
  • 3,528
  • 3
  • 19
  • 20
  • Although the answer pointed by @toATwork works, i prefer this because is more clear the intention of the attribute XmlArrayItem than XmlType for this cases. – E-Bat Jun 18 '14 at 15:17