Automatically created C# classes for xml deserialization don't work

I tried a lot of thing and finaly figured it out. Xml you posted is invalid because xsi:type doesn't work in deserialization.

Valid XML should look like:

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:stn="urn:response">
    <SOAP-ENV:Body>
        <Response>
            <Records>
                <item>
                    <person >John Johnos</person>
                    <address >Some Street 1</address>
                    <age >24</age>
                </item>
            </Records>
            <status>
                <item>
                    <status >success</status>
                    <message/>
                </item>
            </status>
        </Response>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Code then should look like:

XDocument xml = XDocument.Parse(xmlInput);

XmlSerializer serializer = new XmlSerializer(typeof(Response));

using (StringReader stream = new StringReader(items[0].ToString()))
{
    var output = (Response)serializer.Deserialize(stream);
}

Autogenerate class will be from:

<Response>
  <Records  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <item>
      <person>John Johnos</person>
      <address >Some Street 1</address>
      <age>24</age>
    </item>
  </Records>
  <status xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <item >
      <status >success</status>
      <message />
    </item>
  </status>
</Response>

Hope this is clear enough. Not sure how to get rid of types from Envelope so, this is probably not the solution you want.

Method i use for getting things from Envelope is XDocument.Descendants(elemmentName) which return array or List of elements of that name and then you can fill objects. Its more work, but i think its better than transforming xml for deserialization.


Why not generate a serialization library for the entire schema?

  1. Download the XSD schema file from the URL in the message and save it somewhere

    http://schemas.xmlsoap.org/soap/encoding/

  2. Open a Visual Studio Command Prompt and enter the following command

    xsd /classes SoapEncoding.xsd

  3. The output will be a file titled SoapEncoding.cs.

  4. Import this file into your project and try to deserialize the message again.

If all goes well, everything should work this time around.