views:

266

answers:

4

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?

A: 

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

Abraham Durairaj
A: 

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
-1: Have you tried this? It does not work. It will return that XML inside of a SOAP envelope.
John Saunders
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
@User: Never, ever, depend on that test page. Among other things, it does not invoke the methods using SOAP!
John Saunders
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
+1  A: 

Look at using the [ScriptMethod] attribute.

John Saunders
+1  A: 

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
I'm considering that option as well. But I'm still interested in the answer to this question.
User