如何从asmx Web服务返回纯XML?

时间:2022-10-30 23:23:30

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

我想要一个带有GetPeople()方法的asmx webservice,它返回以下XML(不是SOAP响应):

<People>

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

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

</People>

How can I do this?

我怎样才能做到这一点?

4 个解决方案

#1


3  

Look at using the [ScriptMethod] attribute.

查看使用[ScriptMethod]属性。

#2


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?

如果您不希望响应位于SOAP信封中,您是否也不想使用SOAP调用Web服务。例如你不是创建代理类Web引用等,只是使用http post或调用Web服务?

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.

如果是这样,而不是编写Web服务,请编写ASHX处理程序文件。然后,您可以简单地将Response.ContentType设置为text / xml并执行Response.Write(XmlDocument.ToString())。这将返回纯粹的未填充XML以及相关的http标头。

#3


2  

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

我看到我可以将方法的返回类型设置为XmlDocument。这似乎有效。

[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;
}

#4


1  

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

您可以使用Soap Extensions来创建/自定义您的需求。

#1


3  

Look at using the [ScriptMethod] attribute.

查看使用[ScriptMethod]属性。

#2


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?

如果您不希望响应位于SOAP信封中,您是否也不想使用SOAP调用Web服务。例如你不是创建代理类Web引用等,只是使用http post或调用Web服务?

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.

如果是这样,而不是编写Web服务,请编写ASHX处理程序文件。然后,您可以简单地将Response.ContentType设置为text / xml并执行Response.Write(XmlDocument.ToString())。这将返回纯粹的未填充XML以及相关的http标头。

#3


2  

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

我看到我可以将方法的返回类型设置为XmlDocument。这似乎有效。

[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;
}

#4


1  

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

您可以使用Soap Extensions来创建/自定义您的需求。