views:

49

answers:

1

I have a REST web method in .NET 3.5:

[OperationContract]
[WebInvoke(UriTemplate = "", Method = Verbs.Get)]
public string GetBar()
{
   return "foo";
}

The response gets formatted as <string>foo</string> while I would prefer <bar>foo</bar> instead. Does anyone know how to do that? I feel I am missing something obvious.

+1  A: 

When returning XML from a WCF service you usually use one of the serializers, either the DataContractSerializer or the XmlSerializer. I was not able to get the DataContractSerializer to return what you are looking for but here is one way that works with the XmlSerializer.

 [WebGet(UriTemplate = "bar")]
 [OperationContract]
 public bar GetData() {
   return new bar(); 
 }



  [XmlRoot(Namespace = "")]
    public class bar : IXmlSerializable {

        public void WriteXml(XmlWriter writer) {
            writer.WriteString("foo");
        }

        public XmlSchema GetSchema() {
            throw new NotImplementedException();
        }

        public void ReadXml(XmlReader reader) {
            throw new NotImplementedException();
        }

    }

Eventually I just gave up trying to get the serializers to do what I wanted and just started returning "stream" from my contracts and doing the XML generation myself.

Darrel Miller