2

We are facing multiple issues with xml searilization of ASP.NET WebAPI. Currently we are using xmlserializer instead of DataContractSearilizer (which is a default serialization method for XML serialization) for two basic reasons.

  1. In DataContractSearlizer We need to pass all the fields of object in ascending order otherwise getting null at api end.

  2. We also need to pass namespace in XML header for passing XML object to web api.

For these two basic reasons we decided to go for xmlserializer instead of DataContractSearlizer. But now we are facing another issue which was not present in default DataContractSearilization mechanism. Let say we have an object like this

[DataContract]
public class Car
{
    public int ID;

    [DataMember]
    public string CarName;

    [DataMember]
    public string CarType;

}

Now we were expecting that on client end we will only receive the those fields surrounded by DataMember attribute but unexpectedly we are getting all the fields whether DataMember attribute present or absent. This is perfectly working fine in json searlization and result was what we were initially expected. Can someone help us in this regard?

1
  • This might help you Commented Aug 5, 2016 at 12:33

1 Answer 1

1

You are probably looking for the IgnoreDataMember Attribute:

When applied to the member of a type, specifies that the member is not part of a data contract and is not serialized.

[DataContract]
public class Car
{
    [IgnoreDataMember]
    public int ID;

    [DataMember]
    public string CarName;

    [DataMember]
    public string CarType;

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

7 Comments

I think I have already tried this but it was also not working. Let me try one more time. Get back to you after few mins.
Oh, wait - try [XmlIgnore] since you are not using DataContractSerializer - I was to fast...
You are welcome. Please consider accepting the answer.
Although my issue has been resolved now but do you have any idea that why in DataContractSearilizer we need to pass all the fields in ascending order otherwise getting null in field values?
Sorry I dont know initially how to mark an answer as accepted. after read ur refer link i have marked your answer as accepted
|

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.