I am building a RESTful Web Service with the new Microsoft MVC 4 ApiController class and WebAPI. I have a Person class:
public class Person
{
public string surname { get; set; }
public string name{ get; set; }
}
and the default HTTP GET method works, returning the following:
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<surname>John</surname>
<name>Titor</name>
</Person>
I now need an annotation set which lets me change the default inner objects' names, for example I'd like to change surname into msurname. I have tried adding the following:
[XmlElement("msurname")]
annotation, but that only works if the Accept header of my request contains application/xml (of course). I have tried and used the [DataMember] annotation, which is completely ignored.
Is there an annotation set I can use with this ApiController in WebAPI for serialization into both XML and JSON formats? Thank you.
EDIT: correction, if I use the [DataMember] and [DataContract] annotation, I get the desired behaviour with JSON serialization, but not with the XML. The opposite thing happens if I use [XmlElement].