E-Bat
E-Bat

Reputation: 4892

Xml serialization of objects with array property

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?

Upvotes: 0

Views: 631

Answers (1)

Aik
Aik

Reputation: 3738

You have to add XmlArrayItem attribute to the Servos property:

 [XmlArrayItem( "servo" )]
 [XmlArray("servos")]
 public List<Servo> Servos { 
     get;
     set ;
     }
 }

Upvotes: 3

Related Questions