0

I'm working on developing an API using WebAPI2. One of its role is to produce a state required report, which needs to be in XML format.

I'm using a set of XSD's and auto-generated classes. I have an Enum property...

[System.Xml.Serialization.XmlTextAttribute()]
public CrewMemberRoles Value

When I make this enum nullable...

[System.Xml.Serialization.XmlTextAttribute()]
public CrewMemberRoles? Value

It forces WebAPI2 to return JSON instead of XML; Even though I have...

Content-Type: application/xml
Accept: application/xml

...In my HTTP headers.

I tried...

Configuration.Formatters.Remove(Configuration.Formatters.JsonFormatter);

...before the controller returns but WebAPI then returns a totally empty response.

Before I changed the enum to nullable, WebAPI returns the enum as the very first enum in the sequence, or the previous value used for this particular field in the previous call.

Any suggestions are appreciated.

Josh
  • 35
  • 4

1 Answers1

0

To begin with, whenever there's a problem with serialization, the serialization defaults back to the default serializer. For reasons I don't understand, further explained here, there was an issue serializing the nullable.

I solved my issue by creating a bool with the name as XXXSpecified...

[XmlIgnore]
public bool ValueSpecified { get; set; }

And setting this value to false whenever I have a null value in my Value property.

This Stack Overflow question helped me get to that conclusion.

Pang
  • 9,564
  • 146
  • 81
  • 122
Josh
  • 35
  • 4