代码之家  ›  专栏  ›  技术社区  ›  Fernando

从Web服务响应中删除命名空间声明

  •  1
  • Fernando  · 技术社区  · 14 年前

    我有一个Web服务,它返回一个简单的对象:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.4927")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.SoapTypeAttribute(Namespace="urn:CHAMADO")]
    public partial class STRUCCHAMADOOUT : object, System.ComponentModel.INotifyPropertyChanged {
    
        private string cODField;
    
        private string mSGField;
    
        /// <remarks/>
        public string COD {
            get {
                return this.cODField;
            }
            set {
                this.cODField = value;
                this.RaisePropertyChanged("COD");
            }
        }
    
        /// <remarks/>
        public string MSG {
            get {
                return this.mSGField;
            }
            set {
                this.mSGField = value;
                this.RaisePropertyChanged("MSG");
            }
        }
    
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    
        protected void RaisePropertyChanged(string propertyName) {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null)) {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }
    

    这个类是由wsdl.exe根据客户机提供的wsdl文件生成的。这是web方法:

    [WebMethod(MessageName="CHAMADORequest")]
    [SoapRpcMethod(
     Action = "urn:CHAMADO#CHAMADO",
     RequestNamespace = "urn:CHAMADO",
     RequestElementName = "CHAMADO",
     ResponseNamespace = "",
     ResponseElementName = "return",
     Use = SoapBindingUse.Literal
    )]
    [return: XmlElement("return")]
    public STRUCCHAMADOOUT CHAMADO(STRUCCHAMADOIN ENTRADA)
    {
        STRUCCHAMADOOUT result = new STRUCCHAMADOOUT();
        try {
            string str = Util.GetRequestXML();
            persist(getResult<Entidades.Chamado>(str, "ENTRADA", string.Empty));
    
            result.COD = "1";
            result.MSG = "Operação realizada com sucesso";
        } catch (Exception ex) {
            result.COD = "0";
            result.MSG = ex.Message + Environment.NewLine + ex.StackTrace;
        }
    
        return result;
    }
    

    客户机说他的系统正在引发一个错误,因为服务响应具有名称空间声明,如下所示:

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
    <CHAMADOResponse xmlns="urn:CHAMADO" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
        <return xmlns="">
            <COD xmlns="urn:CHAMADO">1</COD> 
            <MSG xmlns="urn:CHAMADO">Operação realizada com sucesso</MSG> 
        </return>
    </CHAMADOResponse>
    

    现在,我通过应用attriute从cod和msg中删除了名称空间。 WebServiceBinding(ConformsTo = WsiProfiles.None) 服务级别和设置 ResponseNamespace 空字符串。但是 CHAMADOResponse 仍然有名称空间声明。 我很确定不应该这样做。事实上,我根本不认为名称空间是问题所在。这个项目从一开始就很困难,因为我们必须创建与遗留wsdl匹配的服务。

    我的问题是:有没有方法可以从web服务响应中删除所有的名称空间声明?

    编辑

    我已经记录了服务器的响应,得到了以下信息:

    <?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>
            <CHAMADOResponse>
                <return>
                    <COD>1</COD>
                    <MSG>Operação realizada com sucesso</MSG>
                </return>
            </CHAMADOResponse>
        </soap:Body>
    </soap:Envelope>
    

    消息正文中没有命名空间声明。但客户说他收到了这个:

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <!-- Request Message Mapping --> 
    <CHAMADOResponse xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
        <return>
            <COD>1</COD>
            <MSG>Operação realizada com sucesso</MSG>
        </return>
    </CHAMADOResponse>
    

    我是不是丢了什么东西?

    1 回复  |  直到 14 年前
        1
  •  0
  •   Tedford    14 年前

    费尔南多

    我遇到过一个类似的情况,在我们的遗留系统(universe)中,它没有真正的xml解析器,而是通过原始字符串匹配工作的自制系统。我们找到的解决方案是在将要传输到系统的类中实现IXMLSerializable,并且在向XmlWriter写入时,我们可以控制添加哪些命名空间。下面是其中一个实现的示例。

    /// <summary>
    /// Serialize the state to XML
    /// </summary>
    /// <param name="writer">The writer to write the state</param>
    public void WriteXml(XmlWriter writer)
    {
        writer.WriteElementString("TERMINATION_STATUS", Status);
        writer.WriteElementString("TRANS_SEQ_NUM", Sequence.ToString());
        writer.WriteElementString("INTRN_SEQ_NUM", Id.ToString());
        writer.WriteElementString("CMRCL_FLAG", IsCommercialCard.ToString());
        writer.WriteElementString("AUTH_CODE", Authorization);
        writer.WriteElementString("CMRCL_TYPE", CommericalFlag.ToString());
        writer.WriteElementString("RESULT_CODE", ResultCode.ToString());
        writer.WriteElementString("TROUTD", RoutingId.ToString());
        writer.WriteElementString("RESPONSE_TEXT", Message);
        writer.WriteElementString("REFERENCE", ProcessorReferenceCode);
        writer.WriteElementString("PAYMENT_MEDIA", PaymentMedia);
        writer.WriteElementString("RESULT", Result.ToString().ToUpper());
        if (Error != null)
        {
            writer.WriteStartElement("ERROR");
            writer.WriteElementString("ERROR_CODE", Error.Code);
            writer.WriteElementString("ERROR_DESCRIPTION", Error.Description);
            writer.WriteEndElement();
        }
    }
    

    再说一次,因为这是一个习惯的情景,我没有帮助,但它是很容易尝试。