0

I have a DTO and a Domain project in my solution and an MVC front end with web api to expose data.

I have the web api controller set up and the action is getting my DTO object back from the DataService. That's all great, however, I want xml to be returned and I want some of the values to be in xml attributes e.g.

<root xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<lookups>
<lookup category="General" field="Alert Type" value="Lack of Transparency" entityid="2273"/>
<lookup category="General" field="Alert Type" value="Unfair Terms " entityid="2274"/>
<lookup category="General" field="Alert Type" value="Operator Concerns" entityid="2275"/>
...
</lookups>
<paymentmethods />
<affiliates />
</root>

The Lookup class is as follows:

[Serializable]
[XmlSerializerFormat]
public class Lookup
{
    [XmlAttribute("category")]
    public String Category { get; set; }

    [XmlAttribute("field")]
    public String Field { get; set; }

    [XmlAttribute("value")]
    public String Value { get; set; }

    [XmlAttribute("entityid")]
    public String EntityId { get; set; }

    public Lookup(String Category, String Field, String Value, int? EntityId = null)
    {
        this.Category = Category;
        this.Field = Field;
        this.Value = Value;
        this.EntityId = (EntityId != null ? EntityId.ToString() : null);
    }

    public Lookup() { }
}

initially I had my DTO objects (e.g Lookup, PaymentMethod and Affiliates) with my viewmodels, but moved them into my DTO project.

I have set UseXmlSerializer = true in my global.asax

Before moving the objects from hy viewmodels folder to the DTO project, it was working and I was getting the desired XML. AFTER moving, it appears to be ignoring the XmlSerializerFormat and using DataContractSerliazer.

So using DataMember attributes, I can format the xml, but obviously I can't set some properties to be serialised as xml attributes

Any thoughts on why it seems to be ignoring the [XmlSerializerFormat] and [XmlAttribute("field")] attributes?

I've read a few SO posts like: XmlSerializer ignores [XmlAttribute] in WebApi and
How can you control .NET DataContract serialization so it uses XML attributes instead of elements?

Community
  • 1
  • 1
Adam Hey
  • 1,512
  • 1
  • 20
  • 24
  • found the solution to this here: http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization#xml_pertype setting the xml Serializer to work on a specific type worked for me – Adam Hey Apr 01 '16 at 12:00

1 Answers1

0

I found the solution to this here: http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization#xml_pertype

Setting the xml Serializer in the Global.asax to work on a specific type worked for me :

var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;
xml.SetSerializer<FullResponseRoot>(new XmlSerializer(typeof(FullResponseRoot)));

where FullResponseRoot is the name of the object that I am serialising (the classes mentioned above are properties of FullResponseRoot) FullResponseRoot forms the root node of the generated xml

Adam Hey
  • 1,512
  • 1
  • 20
  • 24
  • Almost a year later and I experience the same problem only to find my own solution! Lesson: Even though it works, this is clearly not the best solution. Having to explicitly configure the object to be serialised in the global.asax is actually a crap solution. It was too easy to overlook when looking at the global.asax There has to be a more elegantly-configurable solution – Adam Hey Mar 30 '17 at 15:11