1

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 namespaces, 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.

santosh singh
  • 27,666
  • 26
  • 83
  • 129
biffen
  • 161
  • 2
  • 9

2 Answers2

1

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.

biffen
  • 161
  • 2
  • 9
0

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
        }
}
Darrel Miller
  • 139,164
  • 32
  • 194
  • 243