Monday, November 10, 2008

.Net 2.0 Web Services: Raw Text Response

@YaronNaveh

Let's say you want your .Net 2.0 web service to return a simple string without any wrapping element (except the necessary minimum). Like this for example:


<soap:Body>
  <HelloWorldResponse xmlns="http://tempuri.org/">
   Hello World
  </HelloWorldResponse>
</soap:Body>


If you will use the default service template:


[WebMethod]
public string HelloWorld() {
return "Hello World";
}


You'll get a string wrapped with an element "HelloWorldResult":


<soap:Body>
  <HelloWorldResponse xmlns="http://tempuri.org/">
   <HelloWorldResult>
    Hello World
   </HelloWorldResult>
  </HelloWorldResponse>
</soap:Body>



So how to return a raw unwrapped text?
Just add the "return" attribute in this way:



[WebMethod]
[return: System.Xml.Serialization.XmlText()]
public string HelloWorld() {
return "Hello World";
}

@YaronNaveh

What's next? get this blog rss updates or register for mail updates!

0 comments: