Marc Bernier
Marc Bernier

Reputation: 2996

WCF Array Serialization - REST

I have a REST web service that returns a structure containing an array of more structures. This return structure looks like this:

[DataContract]
public class Response {
  private ResponseRecord[] m_Record;

  [DataMember]
  public int TotalRecords { get; set; }

  [DataMember]
  public ResponseRecord[] Record {
    get { return m_Record; }
    set { m_Record = value; }
  }
}

The ResponseRecord class is like this:

[DataContract(Name="Record")]
public class ResponseRecord {
  [DataMember(Order = 0)]
  public string RecordID { get; set; }
/* Many more objects */
}

My web service returns XML like this:

<Response>
  <TotalRecords>1</TotalRecords>
  <Record>
    <ResponseRecord>
      <RecordID>1</RecordID>
      ... Many more objects ...
    </ResponseRecord>
  </Record>
</Response>

What I would like to do is get rid of that "ResponseRecord" hierarchal level, as it adds no new information. This web service also runs for SOAP and XML, and the (Name="Record") attribute did the trick. Not so for REST for some reason, though. Why?

Upvotes: 0

Views: 1521

Answers (2)

Marc Bernier
Marc Bernier

Reputation: 2996

Turns out my array was set up incorrectly:

[DataContract]
public class Response {
  private ResponseRecord[] m_Record;

  [DataMember]
  public int TotalRecords { get; set; }

  [System.Xml.Serialization.XmlElementAttribute("Record")]   // <-- HERE
  public ResponseRecord[] Record {
    get { return m_Record; }
    set { m_Record = value; }
  }
}

This removed the ResponseRecord level, which is what I wanted gone.

Upvotes: 0

Aliostad
Aliostad

Reputation: 81700

First of all, I suggest you change Record property to Records as records is really what it is.

Also, if you remove ResponseRecord, there won't be anything to group the properties of each ResponseRecord instance together. So it is not possible.

Upvotes: 1

Related Questions