4

I have an asp.net web api project and I'm having an issue with User Model which has string[] Roles as property which is not serialized as i want.

User Model:

[DataContract(Namespace = "")]
public class User
{
    [DataMember(Order = 0)]
    public string UserName { get; set; }  //ro

    [DataMember(Order = 1)]
    public int CustomerID { get; set; } //ro

    [DataMember(Order = 2)]
    public string Email { get; set; }

    [DataMember(Order = 3)]
    public string Phone { get; set; }

    [DataMember(Order = 4)]
    public string Mobile { get; set; }

    [DataMember(Order = 5)]
    public string FullName { get; set; }

    [DataMember(Order = 6)]
    public List<string> Roles { get; set; }

    [DataMember(Order = 7)]
    public string Culture { get; set; }

    [DataMember(Order = 8)]
    public string Language { get; set; }

    [DataMember(Order = 9)]
    public string TimeZone { get; set; }

    [DataMember(Order = 10)]
    public DateTime Created { get; set; } //ro

}

and when I get the result for request it is something like as following:-

<User xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <UserName>User1</UserName>
  <CustomerID>1234</CustomerID>
  <Email>abc@xyz.lmn</Email>
  <Phone/>
  <Mobile/>
  <FullName>XYZ</FullName>
  <Roles xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <d2p1:string>AdminRole</d2p1:string>
    <d2p1:string>AlertsRole</d2p1:string>
  </Roles>
  <Culture>en-IN</Culture>
  <Language>en</Language>
  <Created>2014-04-21T11:26:22</Created>
</User>

but I want the xml as follows:-

<User xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <UserName>User1</UserName>
  <CustomerID>1234</CustomerID>
  <Email>abc@xyz.lmn</Email>
  <Phone/>
  <Mobile/>
  <FullName>XYZ</FullName>
  <Roles>
    <string>Role1</string>
    <string>Role2</string>
  </Roles>
  <Culture>en-IN</Culture>
  <Language>en</Language>
  <Created>2014-04-21T11:26:22</Created>
</User>

Kindly suggest any solution which i can implement.

Thanks in advance

Styxxy
  • 7,462
  • 3
  • 40
  • 45
Aryan Agrawal
  • 57
  • 1
  • 7

1 Answers1

5

You can do it by creating a collection type with the same namespace:

[CollectionDataContract(Namespace = "")]
public class StringList : List<string>
{
    public StringList() { }

    public StringList(IEnumerable<string> items) : base(items) { }
}

[DataContract(Namespace = "")]
public class User
{
    [DataMember(Order = 0)]
    public string UserName { get; set; }  //ro

    [DataMember(Order = 1)]
    public int CustomerID { get; set; } //ro

    [DataMember(Order = 2)]
    public string Email { get; set; }

    [DataMember(Order = 3)]
    public string Phone { get; set; }

    [DataMember(Order = 4)]
    public string Mobile { get; set; }

    [DataMember(Order = 5)]
    public string FullName { get; set; }

    [DataMember(Order = 6)]
    public StringList Roles { get; set; }

    [DataMember(Order = 7)]
    public string Culture { get; set; }

    [DataMember(Order = 8)]
    public string Language { get; set; }

    [DataMember(Order = 9)]
    public string TimeZone { get; set; }

    [DataMember(Order = 10)]
    public DateTime Created { get; set; } //ro
}
Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156
  • I want to handle the Post scenario too... But I'm getting my Roles object always null. I've tried StringList : List and also List. In the request body, it Role1. But still my Roles object is not populated. Could any one help me please? – Aryan Agrawal Sep 15 '14 at 07:41
  • 1
    If you inherit from a generic type (e.g. `Collection`), then you also have to use the [`KnownType` attribute](http://stackoverflow.com/a/4875229/393280). – palswim Apr 29 '16 at 20:49