Daniel Borg
Daniel Borg

Reputation: 31

REST service, List returns with ArrayOf Prefix

I'm really new to coding .NET. I've been trying to create a rest web service.

Here is the code:

namespace RestService
{
    [ServiceContract]
    public class RestServiceImpl
    {
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "foo")]
        List<Data> test()
        {
            OrderList ol = new OrderList();
            List<Data> comments = new List<Data>();
            comments.Add(new Data { Name = "sdf" });
            comments.Add(new Data { Name = "sdf" });
            ol.Order = comments;
            return ol.Order;
        }

       [CollectionDataContract]
       public class OrderList: List<Data>
       {
           [DataMember]
           public List<Data> Order{ get; set; }
       }

       [DataContract(Name = "Data")]
       public class Data
       {
           [DataMember]
           public string Name;
       }
   }
}

here is the output I get

<ArrayOfData>
    <Data>
        <Name>sdf</Name>
    </Data>
    <Data>
        <Name>sdf</Name>
    </Data>
</ArrayOfData>

What I'd expected to get was

<OrderList>
    <Data>
        <Name>sdf</Name>
    </Data>
    <Data>
        <Name>sdf</Name>
    </Data>
</OrderList>

hope someone can tell me what I'm missing.

EDIT: if I change the code so I return the orderlist obj instead of just orders facepalm

  OrderList test()
  {
        OrderList ol = new OrderList();
        List<Data> comments = new List<Data>();
        comments.Add(new Data { Name = "sdf" });
        comments.Add(new Data { Name = "sdf" });
        ol.Order = comments;
        return ol;
   }

[CollectionDataContract (Name = "OrderList")]
    public class OrderList: List<Data>
    {

       [DataMember]
       public List<Data> Order{ get; set; }

    }

it still doesn't give me what i'd expect it just gives med the following:

<OrderList/>

and no elements in the list, help is still appricated

Upvotes: 3

Views: 1002

Answers (2)

Jocke
Jocke

Reputation: 2294

Set the Name and ItemName property. See MSDN.

Upvotes: 0

Sleiman Jneidi
Sleiman Jneidi

Reputation: 23349

Try using the Name property.

   [CollectionDataContract(Name="OrderList")]
   public class OrderList: List<Data>

Upvotes: 2

Related Questions