【问题标题】:How to remove extra result tag from my SOAP response如何从我的 SOAP 响应中删除额外的结果标签
【发布时间】:2016-05-16 06:36:55
【问题描述】:

我知道以前有人问过这个问题,但我在任何地方都找不到答案。

问题是我的 asmx 文件中有以下代码:

namespace IrancellSmsServer
{
    [SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
    [WebService(Namespace =   "http://www.csapi.org/schema/parlayx/data/sync/v1_0/local")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    public class SoapServer : System.Web.Services.WebService
    {
        [WebMethod]
        public syncOrderRelationResponse syncOrderRelation(
            Sync.UserID userID,
            string spID,
            string productID,
            string serviceID,
            string serviceList,
            int updateType,
            string updateTime,
            string updateDesc,
            string effectiveTime,
            string expiryTime,
            item[] extensionInfo
            )
        {    
            syncOrderRelationResponse a = new syncOrderRelationResponse();
            a.result = 0;
            a.resultDescription = "OK";         
            return a;
        }
     }
}

结果如下:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <syncOrderRelationResponse xmlns="http://www.csapi.org/schema/parlayx/data/sync/v1_0/local">
      <syncOrderRelationResult>   //dont want this
        <result>0</result>
        <resultDescription>OK</resultDescription>
      </syncOrderRelationResult>  //dont want this
    </syncOrderRelationResponse>
  </soap:Body>
</soap:Envelope

问题是我不想要.net 自动创建的额外&lt;syncOrderRelationResult&gt; 标记。我希望它完全删除。我试过 [SoapDocumentMethod(ParameterStyle=SoapParameterStyle.Bare)]

但运气不好,它告诉我我应该有一个参数。有什么办法可以做到吗?

预期结果:

 <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <soap:Body>
        <syncOrderRelationResponse xmlns="http://www.csapi.org/schema/parlayx/data/sync/v1_0/local">
            <result>0</result>
            <resultDescription>OK</resultDescription>
        </syncOrderRelationResponse>
      </soap:Body>
    </soap:Envelope

这里是syncOrderRelationResponse的代码:

namespace IrancellSmsServer.Sync {
    using System;
    using System.Web.Services;
    using System.Diagnostics;
    using System.Web.Services.Protocols;
    using System.Xml.Serialization;
    using System.ComponentModel;

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "4.6.1055.0")]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Web.Services.WebServiceBindingAttribute(Name="DataSyncBinding", Namespace="http://www.csapi.org/wsdl/parlayx/data/sync/v1_0/service")]
    public partial class DataSyncService : System.Web.Services.Protocols.SoapHttpClientProtocol {

        private bool useDefaultCredentialsSetExplicitly;

        /// <remarks/>
        public DataSyncService() {
            this.Url = global::IrancellSmsServer.Properties.Settings.Default.IrancellSmsServer_Sync_DataSyncService;
            if ((this.IsLocalFileSystemWebService(this.Url) == true)) {
                this.UseDefaultCredentials = true;
                this.useDefaultCredentialsSetExplicitly = false;
            }
            else {
                this.useDefaultCredentialsSetExplicitly = true;
            }
        }

        public new string Url {
            get {
                return base.Url;
            }
            set {
                if ((((this.IsLocalFileSystemWebService(base.Url) == true) 
                            && (this.useDefaultCredentialsSetExplicitly == false)) 
                            && (this.IsLocalFileSystemWebService(value) == false))) {
                    base.UseDefaultCredentials = false;
                }
                base.Url = value;
            }
        }

        public new bool UseDefaultCredentials {
            get {
                return base.UseDefaultCredentials;
            }
            set {
                base.UseDefaultCredentials = value;
                this.useDefaultCredentialsSetExplicitly = true;
            }
        }


        /// <remarks/>
        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Bare)]
        [return: System.Xml.Serialization.XmlElementAttribute("syncOrderRelationResponse", Namespace="http://www.csapi.org/schema/parlayx/data/sync/v1_0/local")]
        public syncOrderRelationResponse syncOrderRelation([System.Xml.Serialization.XmlElementAttribute("syncOrderRelation", Namespace="http://www.csapi.org/schema/parlayx/data/sync/v1_0/local")] syncOrderRelation syncOrderRelation1) {
            object[] results = this.Invoke("syncOrderRelation", new object[] {
                        syncOrderRelation1});
            return ((syncOrderRelationResponse)(results[0]));
        }


        /// <remarks/>
        public void syncOrderRelationAsync(syncOrderRelation syncOrderRelation1, object userState) {
            if ((this.syncOrderRelationOperationCompleted == null)) {
                this.syncOrderRelationOperationCompleted = new System.Threading.SendOrPostCallback(this.OnsyncOrderRelationOperationCompleted);
            }
            this.InvokeAsync("syncOrderRelation", new object[] {
                        syncOrderRelation1}, this.syncOrderRelationOperationCompleted, userState);
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.6.1055.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.csapi.org/schema/parlayx/data/sync/v1_0/local")]
    public partial class syncOrderRelationResponse {

        private int resultField;

        private string resultDescriptionField;

        //private item[] extensionInfoField;

        /// <remarks/>
        public int result {
            get {
                return this.resultField;
            }
            set {
                this.resultField = value;
            }
        }

        /// <remarks/>
        public string resultDescription {
            get {
                return this.resultDescriptionField;
            }
            set {
                this.resultDescriptionField = value;
            }
        }

        /// <remarks/>
        //[System.Xml.Serialization.XmlArrayItemAttribute("item", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
        //public item[] extensionInfo {
        //    get {
        //        return this.extensionInfoField;
        //    }
        //    set {
        //        this.extensionInfoField = value;
        //    }
        //}
    }



        /// <remarks/>
        public syncOrderRelationResponse Result {
            get {
                this.RaiseExceptionIfNecessary();
                return ((syncOrderRelationResponse)(this.results[0]));
            }
        }
    }
    }
}

【问题讨论】:

  • 你为什么不想要它?它给你带来了什么问题
  • syncOrderRelationResponse 的代码也可能有帮助
  • 病毒因为我的客户想要那个。
  • syncOrderRelationResponse 的邮政编码
  • @Viru 我添加了代码

标签: c# .net xml soap asmx


【解决方案1】:

将此添加到您的方法之上

[WebMethod]
[return: XmlElement("syncOrderRelationResponse")]
[SoapDocumentMethod(ParameterStyle = SoapParameterStyle.Bare)]

【讨论】:

    【解决方案2】:

    我有同样的要求,经过一段时间的挖掘,我找到了一个解决方法,如下所示: 如前所述,要删除结果标签,请执行以下操作:

    [WebMethod]
    [return: XmlElement("syncOrderRelationResponse")]
    [SoapDocumentMethod(ParameterStyle = SoapParameterStyle.Bare)]
    //the syncOrderRelation method goes here ...
    

    这将导致如下输出:

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <soap:Body>
        <syncOrderRelationResponse 
           xmlns="http://www.csapi.org/schema/parlayx/data/sync/v1_0/local">
            <result>0</result>
            <resultDescription>OK</resultDescription>
        </syncOrderRelationResponse>
      </soap:Body>
    </soap:Envelope
    

    如你所愿。但是,由于 ParameterStyle 应用于请求和响应元素,它也会去除请求中的方法名称 syncOrderRelation。

    解决方法: 创建一个名为syncOrderRelation 的类,并将syncOrderRelation 请求方法中的所有参数放入刚刚创建的类中,并使用syncOrderRelation 类的对象作为该方法的参数。 完成!

    【讨论】:

      【解决方案3】:

      实现 SoapCore(如 wcf) 就我而言 写这个

      [MessageContract(IsWrapped = false)]
      

      返回班主任并写下这个

      [return: XmlElement("availabilityResponse")]
      

      到适配器类头

      【讨论】:

      • 您使用的是 XmlSerializer 还是 DataContractSerializer?这对我使用 DataContractSerilizer 不起作用,它导致 XmlSerializer 出错。将[System.ServiceModel.MessageBodyMember] 添加到我的请求/响应对象的属性中修复了 that 错误,但随后根元素成为我的请求/响应对象的第一个属性。
      • 查看github.com/DigDes/SoapCore/issues/550以获得更详细的解释。
      • 我记得应该是datacontract serializor
      【解决方案4】:

      将此添加到您的方法之上

      [WebMethod]
      [return: XmlElement("syncOrderRelationResponse")]
      [SoapDocumentMethod(ParameterStyle = SoapParameterStyle.Bare)]
      

      并将其更改为:

      [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
      

      到:

      [WebServiceBinding(ConformsTo = WsiProfiles.None)]
      

      你也可以使用这个链接:哇!

      https://stackoverflow.com/a/29855766/8517391

      【讨论】:

        【解决方案5】:

        无法删除此结果。至少没有记录。

        但是您可以通过 return 属性修改此名称:

        我知道这可能对您没有直接帮助(因为您想展平结构),但是由于这不是真正的标准方式,因此知道您至少可以修改结果标签。

        【讨论】:

          【解决方案6】:
          1. 如果您想将 &lt;syncOrderRelationResult&gt; 更改为 &lt;carResult&gt;,只需将 [XmlRoot("carResult")] 添加到您的对象,而不是你的函数/方法。这是示例:

          [XmlRoot("carResult")] public class ObjectCar { public string model; public string color; }

          或将 [return: System.Xml.Serialization.XmlElementAttribute("carResult")] 添加到您的函数/方法中:

          [WebMethod] [return: System.Xml.Serialization.XmlElementAttribute("carResult")] public string syncOrderRelation(string a) { return a; }

          1. 如果您想将 &lt;syncOrderRelationResponse ..&gt; 更改为 &lt;YourFunctionName ..&gt;,只需将 [SoapDocumentMethodAttribute(ResponseElementName = "YourFunctionName")] 添加到你的功能/方法:

          [WebMethod] [return: System.Xml.Serialization.XmlElementAttribute("carResult")][SoapDocumentMethodAttribute(ResponseElementName = "YourFunctionName")] public string syncOrderRelation(string a) { return a; }

          希望对你有帮助

          【讨论】:

            猜你喜欢
            • 2014-10-16
            • 2018-09-11
            • 2020-01-14
            • 2018-02-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多