Soap Message Deserialization

c#, deserialization, soap

Solution

One alternative is to use WCF to deserialize it - for which you'll need to define a class which maps to the body of your message. The code below shows one way how this can be implemented.

public class StackOverflow_24559375
{
    const string XML = @"<?xml version=""1.0""?>
        <s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
          <s:Body>
            <ShipNo_Check xmlns=""http://bimgewebservices.gmb.gov.tr"">          
              <shipNo>13343100VB0000000014</shipNo>
              <harborNo>1234567</harborNo>
            </ShipNo_Check>
          </s:Body>
        </s:Envelope>";

    [DataContract(Name = "ShipNo_Check", Namespace = "http://bimgewebservices.gmb.gov.tr")]
    public class ShipNo_Check
    {
        [DataMember(Name = "shipNo", Order = 1)]
        public string ShipNo { get; set; }

        [DataMember(Name = "harborNo", Order = 2)]
        public string HarborNo { get; set; }
    }

    public static void Test()
    {
        using (var reader = XmlReader.Create(new StringReader(XML)))
        {
            Message m = Message.CreateMessage(reader, int.MaxValue, MessageVersion.Soap11);
            var body = m.GetBody<ShipNo_Check>();
            Console.WriteLine(body.ShipNo + " - " + body.HarborNo);
        }
    }
}

Problem

I have soap message like this: ``` <?xml version="1.0"?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <ShipNo_Check xmlns="http://bimgewebservices.gmb.gov.tr"> <shipNo>13343100VB0000000014</shipNo> <harborNo>1234567</harborNo> </ShipNo_Check> </s:Body> </s:Envelope> ``` In my asp.net mvc application, I need to serialize this xml. I am using this method: ``` public object SoapTo(string soapString) { IFormatter formatter; MemoryStream memStream = null; Object objectFromSoap = null; try { byte[] bytes = new byte[soapString.Length]; Encoding.ASCII.GetBytes(soapString, 0, soapString.Length, bytes, 0); memStream = new MemoryStream(bytes); formatter = new SoapFormatter(); objectFromSoap = formatter.Deserialize(memStream); } catch (Exception exception) { throw exception; } finally { if (memStream != null) memStream.Close(); } return objectFromSoap; } ``` I got the following error: ``` Parse Error, no assembly associated with Xml key _P1 ShipNo_Check ``` How can I achieve this?

Original source