在C#中反序列化响应肥皂服务

时间:2016-11-11 11:09:18

标签: c# xml serialization

我目前正在接受svc soap服务的回复:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <soap:Fault>
            <faultcode>soap:Client</faultcode>
            <faultstring>[0417] - El identificador de petición y de solicitud no coinciden.</faultstring>
            <faultactor>Seguridad Social</faultactor>
            <detail>
                <Atributos xmlns="http://intermediacion.redsara.es/scsp/esquemas/V3/soapfaultatributos">
                    <IdPeticion>HACSGTIC-2016111056-0036</IdPeticion>
                    <NumElementos>1</NumElementos>
                    <TimeStamp>2016-11-10T11:56:51.687+01.00</TimeStamp>
                    <Estado>
                        <CodigoEstado>0417</CodigoEstado>
                        <LiteralError>[0417] - El identificador de petición y de solicitud no coinciden.</LiteralError>
                        <TiempoEstimadoRespuesta />
                    </Estado>
                    <CodigoCertificado>Q2827002CINSS001</CodigoCertificado>
                </Atributos>
            </detail>
        </soap:Fault>
    </soap:Body>
</soap:Envelope>

我需要反序列化xml属性,因为我有一个“Attributes”对象,其属性对应于xml,然后我把类的属性作为服务的引用:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.6.1064.2")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://intermediacion.redsara.es/scsp/esquemas/V3/peticion")]
public partial class Atributos : object, System.ComponentModel.INotifyPropertyChanged {

    private string idPeticionField;

    private int numElementosField;

    private string timeStampField;

    private Estado estadoField;

    private CodigoCertificado codigoCertificadoField;

    /// <comentarios/>
    public string IdPeticion {
        get {
            return this.idPeticionField;
        }
        set {
            this.idPeticionField = value;
            this.RaisePropertyChanged("IdPeticion");
        }
    }

    /// <comentarios/>
    public int NumElementos {
        get {
            return this.numElementosField;
        }
        set {
            this.numElementosField = value;
            this.RaisePropertyChanged("NumElementos");
        }
    }

    /// <comentarios/>
    public string TimeStamp {
        get {
            return this.timeStampField;
        }
        set {
            this.timeStampField = value;
            this.RaisePropertyChanged("TimeStamp");
        }
    }

    /// <comentarios/>
    public Estado Estado {
        get {
            return this.estadoField;
        }
        set {
            this.estadoField = value;
            this.RaisePropertyChanged("Estado");
        }
    }

    /// <comentarios/>
    public CodigoCertificado CodigoCertificado {
        get {
            return this.codigoCertificadoField;
        }
        set {
            this.codigoCertificadoField = value;
            this.RaisePropertyChanged("CodigoCertificado");
        }
    }

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


[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.6.1064.2")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://intermediacion.redsara.es/scsp/esquemas/V3/peticion")]
public partial class Estado : object, System.ComponentModel.INotifyPropertyChanged {

    private string codigoEstadoField;

    private string codigoEstadoSecundarioField;

    private string literalErrorField;

    private int tiempoEstimadoRespuestaField;

    private bool tiempoEstimadoRespuestaFieldSpecified;

    /// <comentarios/>
    public string CodigoEstado {
        get {
            return this.codigoEstadoField;
        }
        set {
            this.codigoEstadoField = value;
            this.RaisePropertyChanged("CodigoEstado");
        }
    }

    /// <comentarios/>
    public string CodigoEstadoSecundario {
        get {
            return this.codigoEstadoSecundarioField;
        }
        set {
            this.codigoEstadoSecundarioField = value;
            this.RaisePropertyChanged("CodigoEstadoSecundario");
        }
    }

    /// <comentarios/>
    public string LiteralError {
        get {
            return this.literalErrorField;
        }
        set {
            this.literalErrorField = value;
            this.RaisePropertyChanged("LiteralError");
        }
    }

    /// <comentarios/>
    public int TiempoEstimadoRespuesta {
        get {
            return this.tiempoEstimadoRespuestaField;
        }
        set {
            this.tiempoEstimadoRespuestaField = value;
            this.RaisePropertyChanged("TiempoEstimadoRespuesta");
        }
    }

    /// <comentarios/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool TiempoEstimadoRespuestaSpecified {
        get {
            return this.tiempoEstimadoRespuestaFieldSpecified;
        }
        set {
            this.tiempoEstimadoRespuestaFieldSpecified = value;
            this.RaisePropertyChanged("TiempoEstimadoRespuestaSpecified");
        }
    }

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

/// <comentarios/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.6.1064.2")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://intermediacion.redsara.es/scsp/esquemas/V3/peticion")]
public enum CodigoCertificado {

    /// <comentarios/>
    Q2827002CINSS001,
}

这是我的代码,它将xml反序列化为“Attributes”对象:

    public static T Deserializar<T>(string respuesta, string nameSpace, string xpath = "//soap:Envelope/soap:Body/p:Respuesta")
    {


        T objetoDeserializado = default(T);
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(respuesta);

        Dictionary<string, string> nsAdicionales = new Dictionary<string, string>();
        nsAdicionales.Add("s", "http://www.w3.org/2003/05/soap-envelope");
        nsAdicionales.Add("soap", "http://schemas.xmlsoap.org/soap/envelope/");
        nsAdicionales.Add("i", "http://www.w3.org/2001/XMLSchema-instance");
        nsAdicionales.Add("p", nameSpace);
        XmlNamespaceManager nsm = new XmlNamespaceManager(xmlDoc.NameTable);
        foreach (string prefijo in nsAdicionales.Keys)
        {
            nsm.AddNamespace(prefijo, nsAdicionales[prefijo]);
        }
        XmlNode nodoObjeto = xmlDoc.SelectSingleNode(xpath, nsm);
        if (nodoObjeto != null)
        {

            try
            {
                //Se añade el atributo XmlRootAttribute para aquellos casos que esté definido el objeto
                //con un nombre y se utilice con otro.
                XmlRootAttribute xRoot = new XmlRootAttribute();
                xRoot.ElementName = nodoObjeto.Name;
                xRoot.Namespace = nodoObjeto.NamespaceURI;
                var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T), xRoot);
                XmlReader xmlReader = nodoObjeto.CreateNavigator().ReadSubtree();
                var obj = serializer.Deserialize(xmlReader);
                objetoDeserializado = ((T)obj);
            }
            catch (Exception ex)
            {
                respuesta = "Error al intentar deserializar el objeto soap: " + ex.Message;
            }
        }

        return objetoDeserializado;
    }

其中T的类型为“SCSP.ServiciosCompartidos.INSS.ConsultaPrestaciones.Atributes”,它不会给出错误,但是如果我们看到反序列化对象,它会将所有值都设为NULL

请帮忙。

0 个答案:

没有答案