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 "R开发者_如何学编程esponseRecord" 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.
精彩评论