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?

1

1 Answer 1

3

You have to add XmlArrayItem attribute to the Servos property:

 [XmlArrayItem( "servo" )]
 [XmlArray("servos")]
 public List<Servo> Servos { 
     get;
     set ;
     }
 }
Sign up to request clarification or add additional context in comments.

1 Comment

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.

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.