Parse XML to SOAP Envelope and Deserialize Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KusakaKyo
    New Member
    • Feb 2014
    • 1

    Parse XML to SOAP Envelope and Deserialize Error

    Ok, so i have been trying to request some webservices from a PHP server, but the thing is that it should be reciveing SOAP Envelop formated XMLs .... The thing is that when i call their services, i get some errors, so i parsed my XML and edited it to match the envelope format, but the problema is when i try to deserialize the parsed XML and place it in the Request object. Hope it looks clearer with the code:

    This is my parser.

    Code:
    [XmlType(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public class SOAPEnvelope<T>
        {
            [XmlAttribute(AttributeName = "soapenv", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
            public string soapenv { get; set; }
    
            [XmlElement(ElementName = "Header", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
            public ResponseHeader header { get; set; }
    
            [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
            public ResponseBody<T> body { get; set; }
            
            [XmlNamespaceDeclarations]
            public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
            
            public SOAPEnvelope()
            {
                xmlns.Add("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
            }
        }
    
        [XmlRoot(ElementName = "Header", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public class ResponseHeader
        {
        }
    
        [XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public class ResponseBody<T>
        {
            [XmlElement(ElementName = "infoTransferencias", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
            public T Main { get; set; }
        }
    
        public static object XmlToFromFile(string filePath, Type type)
        {
            XmlSerializer xmlSerializer;
            FileStream fileStream = null;
            try
            {
                xmlSerializer = new XmlSerializer(type);
                fileStream = new FileStream(filePath,
                            FileMode.Open, FileAccess.Read);
                object objectFromXml =
                     xmlSerializer.Deserialize(fileStream);
                return objectFromXml;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            finally
            {
                if (fileStream != null) fileStream.Close();
            }
        }
    And this is where i call it after fillinf the object with information.

    Code:
    infoTransRequestAPIService.parameters = infoTransAPIService;
                var se = new SOAPEnvelope<WsAPIService.infoTransferenciasRequest>
                {
                    header = new ResponseHeader
                    {
                        
                    },
                    body = new ResponseBody<WsAPIService.infoTransferenciasRequest>
                    {
                        Main = infoTransRequestAPIService
                    }
                };
                XMLParam = objXML.createDir("IN", 35, "201428100444", "Info_Transferencias");
                var soapserializer = new XmlSerializer(typeof(SOAPEnvelope<WsAPIService.infoTransferenciasRequest>));
                TextWriter soapwriter = new StreamWriter(@"" + XMLParam.archivo + @".xml");
                soapserializer.Serialize(soapwriter, se);
                soapwriter.Close();
    All the way to this point there are no problems, i can open my XML and verify it it matches manually, then i try to deserialize with this code and add the deserialized object to the request object.

    Code:
    var soapFormat = @"<?xml version=""1.0"" encoding=""UTF-8""?>
                                   <soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" 
                                        xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" 
                                        xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
                                        <soapenv:Header/>
                                        <soapenv:Body>
                                            <xsi:infoTransferencias>
                                                <xsi:transferencia>
                                                    <xsi:infoTransferenciasIn>
                                                        <xsi:transaccionAduana>201428100444</xsi:transaccionAduana>
                                                        <xsi:contenedorID>BMOU5762067</xsi:contenedorID>
                                                        <xsi:lineaNaviera>10</xsi:lineaNaviera>
                                                        <xsi:tipoContenedor>2</xsi:tipoContenedor>
                                                        <xsi:fechaIngreso>2013/12/06 08:00:00</xsi:fechaIngreso>
                                                        <xsi:vacio>false</xsi:vacio>
                                                        <xsi:candado>Test</xsi:candado>
                                                        <xsi:bultos>2</xsi:bultos>
                                                        <xsi:peso>10</xsi:peso>
                                                        <xsi:embalaje>2</xsi:embalaje>
                                                        <xsi:puertoEmbarque>ZLO</xsi:puertoEmbarque>
                                                        <xsi:sobredimiencionado>false</xsi:sobredimiencionado>
                                                        <xsi:listaBL>
                                                            <xsi:Bl>
                                                                <xsi:folioBl>68443749745</xsi:folioBl>
                                                                <xsi:rsConsignatario>N/A</xsi:rsConsignatario>
                                                                <xsi:rfcConsignatario>N/A</xsi:rfcConsignatario>
                                                                <xsi:domicilioConsignatario>N/A</xsi:domicilioConsignatario>
                                                                <xsi:rsNotificador>N/A</xsi:rsNotificador>
                                                                <xsi:rfcNotificador>N/A</xsi:rfcNotificador>
                                                                <xsi:domicilioNotificador>N/A</xsi:domicilioNotificador>
                                                                <xsi:rsEmbarcador>N/A</xsi:rsEmbarcador>
                                                                <xsi:rfcEmbarcador>N/A</xsi:rfcEmbarcador>
                                                                <xsi:domicilioEmbarcador>N/A</xsi:domicilioEmbarcador>
                                                                <xsi:CodigosPeligrosidad>
                                                                    <xsi:imdg>0</xsi:imdg>
                                                                    <xsi:un>0</xsi:un>
                                                                </xsi:CodigosPeligrosidad>
                                                            </xsi:Bl>
                                                        </xsi:listaBL>
                                                    </xsi:infoTransferenciasIn>
                                                </xsi:transferencia>
                                            </xsi:infoTransferencias>
                                        </soapenv:Body>
                                    </soapenv:Envelope>";
                var Value = XDocument.Parse(soapFormat);
                WsAPIService.infoTransferenciasRequest deserializableObject;
                using (var reader = Value.CreateReader())
                {
                    var ser = new XmlSerializer(typeof(WsAPIService.infoTransferenciasRequest));
                    deserializableObject = (WsAPIService.infoTransferenciasRequest)ser.Deserialize(reader);
                }
                infoTransRequestAPIService = deserializableObject;

    Here is where i get this error:

    Error en el documento XML (0, 0). - en System.Xml.Seri alization.XmlSe rializer.Deseri alize(XmlReader xmlReader, String encodingStyle, XmlDeserializat ionEvents events) en System.Xml.Seri alization.XmlSe rializer.Deseri alize(XmlReader xmlReader) en Test_Varios.Tes t_Click(Object sender, EventArgs e) en c:\inetpub\wwwr oot\WsDesarroll o2\Test_Varios. aspx.cs:lĂ­nea 275 - System.InvalidO perationExcepti on: No se esperaba . en Microsoft.Xml.S erialization.Ge neratedAssembly .XmlSerializati onReaderObject. Read2_anyType()
    Does anyone know why is this happening?, i have no idea and i am starting to get really frustrated. Anyways Thanks guys.
Working...