C# 如何更改ASP.NET Webservice的WebMethod XML输出,特别是命名空间声明?

C# 如何更改ASP.NET Webservice的WebMethod XML输出,特别是命名空间声明?,c#,asp.net,xml,web-services,C#,Asp.net,Xml,Web Services,我正在为给定的客户端开发一个ASP.NET Web服务(不是WCF)。这是其中一种情况,您无法在客户端更改任何内容 客户端发送以下XML以请求方法: <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas

我正在为给定的客户端开发一个ASP.NET Web服务(不是WCF)。这是其中一种情况,您无法在客户端更改任何内容

客户端发送以下XML以请求方法:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                   xmlns:cometxsd="http://werk-ii.de/soap/comet/Schema"
                   xmlns:comet="http://werk-ii.de/soap/comet"
                   xmlns:xop="http://www.w3.org/2004/08/xop/include"
                   xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
                   xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
                   xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
                   xmlns:xmime5="http://www.w3.org/2005/05/xmlmime"
                   xmlns:ns1="http://soap.comet.werkii.com/">
  <SOAP-ENV:Body>
    <ns1:login xsi:type="ns1:login">
      <user>myusername</user>
      <password>mypassword</password>
      <client>whatever</client>
      <language>de</language>
    </ns1:login>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
如果启动服务,它会告诉我必须作为请求发送哪些SOAP 1.1代码,即:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <login xmlns="http://soap.comet.werkii.com/">
      <user>string</user>
      <password>string</password>
      <client>string</client>
      <language>string</language>
    </login>
  </soap:Body>
</soap:Envelope>

一串
一串
一串
一串
当我从另一个测试客户机(我用PHP编写了一个)使用这段代码时——正如服务所告诉的那样——一切正常,我得到了一个结果。但是,当我从一开始就发送代码时(这是真正的客户机将发送的代码),会调用该方法,但所有4个参数都为null

从XML的角度来看,我认为这两个请求是相同的。唯一的区别是,命名空间是在哪里定义的,元素是否使用ns1前缀。当服务将其读取为XML时,这应该没有任何区别。也许我错了

也许第一个XML中的4个参数的名称空间(none)与方法(ns1)不同。这就是为什么所有参数都为空的原因吗?如何仅更改参数的名称空间

当我仅更改XML中的方法行时(将
替换为
以及结束标记),它就工作了!因此,如果method元素使用名称空间前缀,那么服务似乎不理解我的请求,尽管名称空间已在根元素中正确定义

我尝试了以下更改服务所需的XML格式:

  • System.Web.Services.Protocols.SoapDocumentMethodAttribute-完全无效
  • XmlNamespaceDeclarationsAttribute,如图所示,它似乎不起作用,因为它是为处理复杂类型而设计的,而不是服务类或方法

所以问题是,我如何告诉我的服务接受第一个示例中的XML?

很高兴知道参数也可以有属性:

public LoginResult Login (
    [XmlElement(Namespace = "")] string user,
    [XmlElement(Namespace = "")] string password,
    [XmlElement(Namespace = "")] string client,
    [XmlElement(Namespace = "")] string language)
{
    return new LoginResult() {
        ResultCode = 0,
        SessionId = user + "-" + password + "-" + client + "-" + language
    };
}
这就是将参数放入全局名称空间的解决方案——问题已经解决

public LoginResult Login (
    [XmlElement(Namespace = "")] string user,
    [XmlElement(Namespace = "")] string password,
    [XmlElement(Namespace = "")] string client,
    [XmlElement(Namespace = "")] string language)
{
    return new LoginResult() {
        ResultCode = 0,
        SessionId = user + "-" + password + "-" + client + "-" + language
    };
}