I'm trying to serialize an object with an attribute on the root element (which is a collection).
[XmlType("Person")]
public class Person
{
public id Id { get; set; }
public string Name { get; set; }
}
[XmlType("Persons")]
public class Persons : Collection<Person>
{
[XmlAttribute("Nb")]
public int Nb
get {
return this.Count;
}
set { }
}
}
var persons = new Persons();
var person = new Person();
person.Id = 42;
person.Name = "Toto";
persons.Add(person);
return persons;
The serialized output is the following:
<Persons>
<Person>
<Id>42</Id>
<Name>Toto</Name>
</Person>
</Persons>
I'm expecting
<Persons Nb="1">
<Person>
<Id>42</Id>
<Name>Toto</Name>
</Person>
</Persons>
How can I bring the Nb
attribute in ?