简体   繁体   中英

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM