XML format in WCF REST request and response
I have set up a WCF service that will accept both JSON and XML in the same method, and that supports both SOAP and REST.
The JSON works fine, but I do not know how the XML should look.
The interface looks like this:
[ServiceContract]
public interface IWebService
{
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Bare)]
string[] EchoArray(string[] stringArray);
}
If possible, I would like to keep the XML as simple as possible, without n开发者_如何转开发amespaces, like this:
<stringArray>
<string>hello</string>
<string>hola</string>
</stringArray>
The response should be simple as well.
If it makes any difference, I am doing it all in code, without any web.config.
This is so I can use an Azure worker role.
I decided to go with a wrapped request instead of the bare (because another method required it), and figured out how to format it.
First I changed the
[ServiceContract]
to
[ServiceContract(Namespace = "")]
Then this worked:
<EchoArray>
<stringArray xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:string>hello</a:string>
<a:string>hola</a:string>
</stringArray>
</EchoArray>
It would probably work without the wrapped request as well, but for consistency I made this method wrapped as well.
If you want to control what the XML looks like, you could do this:
[ServiceContract]
public interface IWebService
{
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Bare)]
StringArray EchoArray(StringArray stringArray);
}
public class StringArray : IXmlSerializable {
public XmlSchema GetSchema() {
return null;
}
public void ReadXml(XmlReader reader) {
// However you have formatted it
}
public void WriteXml(XmlWriter writer) {
// However you want it formatted
}
}
精彩评论