5

I want an asmx webservice with a method GetPeople() that returns the following XML (NOT a SOAP response):

<People>

    <Person>
        <FirstName>Sara</FirstName>
        <LastName>Smith</LastName>
    </Person>

    <Person>
        <FirstName>Bill</FirstName>
        <LastName>Wilson</LastName>
    </Person>

</People>

How can I do this?

User
  • 62,498
  • 72
  • 186
  • 247

4 Answers4

3

Look at using the [ScriptMethod] attribute.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
3

If you don't want the Response to be in a SOAP envelope, are you also not bothered about calling the web service using SOAP. e.g. you are not creating proxy classes web references etc and just using http post or get to call the web service?

If so rather than writing a web service, write a ASHX handler file. You can then simply set the Response.ContentType to text/xml and do Response.Write(XmlDocument.ToString()). That will return pure unadulaterated XML plus the relevent http headers.

Ben Robinson
  • 21,601
  • 5
  • 62
  • 79
  • I'm considering that option as well. But I'm still interested in the answer to this question. – User May 07 '10 at 16:28
2

I see I can set the return type of the method to XmlDocument. This seems to work.

[WebMethod]
public XmlDocument ReturnXml()
{
    XmlDocument dom = new XmlDocument();

    XmlElement people = dom.CreateElement("People");
    dom.AppendChild(people);

    XmlElement person = dom.CreateElement("Person");
    people.AppendChild(person);

    XmlElement firstName = dom.CreateElement("FirstName");
    person.AppendChild(firstName);

    XmlText text = dom.CreateTextNode("Bob");
    firstName.AppendChild(text);



    // load some XML ...
    return dom;
}
User
  • 62,498
  • 72
  • 186
  • 247
  • -1: Have you tried this? It does not work. It will return that XML inside of a SOAP envelope. – John Saunders May 06 '10 at 23:48
  • yes I did try it, and at least through the "invoke" method on the auto page that is generated it worked hence my post of it. I will try it again to confirm. – User May 07 '10 at 02:06
  • 4
    Never, ever, depend on that test page. Among other things, it does not invoke the methods using SOAP! – John Saunders May 07 '10 at 11:06
  • Now I have tried it not using the test page and it works. To clarify, I'm calling via HTTP POST. I haven't tried a soap request but I don't plan on calling it via SOAP – User May 07 '10 at 20:53
1

You may use Soap Extensions to create / customize for your needs.