How to return pure XML from asmx web service?

I want an asmx webservice with a GetPeople () method 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?

+5
source share
4 answers

I see that I can set the return type of the method in an 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;
}
+2
source

Look at using an attribute [ScriptMethod].

+3
source

, Response SOAP, - SOAP. - - .., HTTP- -?

, -, ASHX. Response.ContentType text/xml Response.Write(XmlDocument.ToString()). unadulaterated XML HTTP.

+3

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

+1
source

All Articles