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

时间:2010-04-30 13:54:15

标签: c# .net web-services

我有一个返回简单对象的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文件。这是网络方法:

[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 WebServiceBinding(ConformsTo = WsiProfiles.None)应用于服务的类并将ResponseNamespace设置为空字符串来从COD和MSG中删除命名空间。但是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 个答案:

答案 0 :(得分:0)

费尔南多,

我遇到了与我们的遗留系统(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();
    }
}

再次,因为它是一个自定义的situtation我没有帮助,但它很容易尝试。