WCF custom XML serialization

2019-05-29 08:10发布

In WCF, if I have code like this and I'm returning it in XML format, the XML is auto generated. Is it possible for me to expicitly specify which XML will be used for the serialization of the CustomerEntity?

[OperationContract]    
[WebInvoke(Method = "GET"]
CustomerEntity GetCustomer(int customerPk);

The alternative that I'm currently using is to return an XElement, but the problem with this is that I can't also support JSon that way.

Update: My types are immutable and use raedonly properties, so IXmlSerializable won't work for me.

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-05-29 08:59

Implement IXmlSerializable on CustomerEntity.

查看更多
The star\"
3楼-- · 2019-05-29 09:04

You can try using Raw messages i.e. using Contract types deriving from Message class and writing the messages the way you want by overriding OnWriteBodyContents and other overridable Message class members. Please refer "Inheriting from the Message Class" section gof following MSDN article to know more about overridable members of Message class, http://msdn.microsoft.com/en-us/library/ms734675.aspx

HTH, Amit

查看更多
登录 后发表回答