0

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?

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I've tried to add something like [DataMember(Name = "property1")] to show serializer that those properties should be serialized, but it not helped. Your solution with config.Formatters worked.
1

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

1 Comment

That was first thing I've tried. In this way Xml output is empty: <MyClass></MyClass>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.