我试图在根元素(这是一个集合)上使用属性序列化一个对象。
[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;
序列化输出如下:
<Persons>
<Person>
<Id>42</Id>
<Name>Toto</Name>
</Person>
</Persons>
我期待
<Persons Nb="1">
<Person>
<Id>42</Id>
<Name>Toto</Name>
</Person>
</Persons>
如何将Nb
属性带入?