I have some XML which I would like to serialize into a class.
<Group>
<Employees>
<Employee>
<Name>Hari</Name>
<Age>30</Age>
</Employee>
<Employee>
<Name>Yougov</Name>
<Age>31</Age>
</Employee>
<Employee>
<Name>Adrian</Name>
<Age>28</Age>
</Employee>
</Employees >
The above XML can be realized in C# pretty much easily.
But I’m stumbled upon my requirement, where the XML looks like,
<Group>
<Employees>
<Hari Age=30 />
<Yougov Age=31 />
<Adrian Age=28 />
</Employees >
</Group>
Where Employees is a List<Employee> with KeyValuePair<string, int>("Hari", 30)
How do I design the classes and member variables to get the above serialized XML?
(Provided, there wont be any duplicate names in the employee list)
Any help is much appreciated.
It is not a good idea to use the data as the schema; in particular, an awful lot of names are not valid as xml element names, but also: it just isn’t good practice (for example, it makes it virtually useless in terms of schema validation). If you want to be terse, maybe something like:
or
which can be done simply with:
and:
XmlSerializerdoes not support the “content as an element name” serializer, unless you do everything yourself withIXmlSerializablefrom the parent element (it would have to be from the parent, asXmlSerializerwould have no way of identifying the child to handle there).