m03geek
m03geek

Reputation: 2538

Asp.net MVC4 Web API XML serialization ignores public properties

I'm using asp.net mvc4 web api. I've got some classes generated by Devart Entity Developer and they have following structure:

[Serializable]
[XmlRoot("Test")]
[JsonObject(MemberSerialization.OptIn)]
public class Test
{
    [XmlAttribute("property1")]
    [JsonProperty("property1")]
    public int Property1
    {
        get { return _Property1; }
        set
        {
            if (_Property1 != value)
            {
                _Property1 = value;
            }
        }
    }
    private int _Property1;

    [XmlAttribute("property2")]
    [JsonProperty("property2")]
    public int Property2
    {
        get { return _Property2; }
        set
        {
            if (_Property2 != value)
            {
                _Property2 = value;
            }
        }
    }
    private int _Property2;
}

I have test controller for such class:

public class TestController : ApiController
{
    private List<Test> _tests = new List<Test>() ;

    public TestController()
    {
        _tests.Add(new Test() { Property1 = 1, Property2 = 2 });
        _tests.Add(new Test() { Property1 = 3, Property2 = 4 });
    }

    public IEnumerable<Test> Get()
    {
        return _tests;
    }
}

When I try to get test values in JSON format it returns correct response:

"[{"property1":1,"property2":2},{"property1":3,"property2":4}]"

But when I use XML format it serializes not public (Property1) but private properties (i.e. _Property1) and response look like:

<ArrayOfTest xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/TestProject.Models.Data">
  <Test>
     <_Property1>1</_Property1>
     <_Property2>2</_Property2>
   </Test>
   <Test>
     <_Property1>3</_Property1>
     <_Property2>4</_Property2>
   </Test>
</ArrayOfTest>

UPD: I've tried to add [NonSerialized] and [XmlIgnore] to private properties, but in this way xml output was empty, just:

<ArrayOfTest xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/PeopleAirAPI.Models.Data">
  <Test/>
  <Test/>
</ArrayOfTest>

The question is how to force xml serializator to serialize public properties. To hide (ignore) that private properties is not a problem. I can't understand why it serializes that private properties at all, I've read in msdn docs and in other places that:

XML serialization only serializes public fields and properties.

Why in this case it acts contrary to docs?

Upvotes: 0

Views: 3292

Answers (2)

Youssef Moussaoui
Youssef Moussaoui

Reputation: 12395

Web API uses DataContractSerializer instead of XmlSerializer by default, which looks at [Serializable] and serializes all fields before looking at anything else.

It looks like your type was designed to be serialized using XmlSerializer. I would suggest adding the following line:

config.Formatters.XmlFormatter.UseXmlSerializer = true;

This will ensure all the public properties get serialized and all the XML serialization attributes, like [XmlAttribute] get respected.

Upvotes: 5

thangchung
thangchung

Reputation: 2030

Trying to put [XmlIgnore] on the private long _Id;

Upvotes: 1

Related Questions