1

I'm trying to deserialize an xml response I get from a call. I've used Visual Studios "Paste Special" function to prepare my program for how the file is gonna look, and the only change I've made to the result of this is changing a field from private to public, so that I can access it in my code. (This is done outside of the snippet of code, just want to clarify that I've not made any changes in the code snippet)

The error

System.InvalidOperationException: 'There is an error in XML document (1, 2).'
Inner Exception
InvalidOperationException: <Envelope xmlns='http://schemas.xmlsoap.org/soap/envelope/'> was not expected.

Start of my class

[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
public partial class Envelope
{

    private object headerField;

    private EnvelopeBody bodyField;

    /// <remarks/>
    public object Header
    {
        get
        {
            return this.headerField;
        }
        set
        {
            this.headerField = value;
        }
    }

    /// <remarks/>
    public EnvelopeBody Body
    {
        get
        {
            return this.bodyField;
        }
        set
        {
            this.bodyField = value;
        }
    }
}

Start of my XML

<SOAP:Envelope xmlns:SOAP='http://schemas.xmlsoap.org/soap/envelope/'>
    <SOAP:Header/>
    <SOAP:Body xmlns:wsu='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' xmlns:urn='urn:oio:medarbejder:1.0.0' xmlns:urn1='urn:oio:sagdok:3.0.0'>
        <ns0:ListOutput xmlns:ns0='urn:oio:medarbejder:1.0.0'>
            <ns1:StandardRetur xmlns:ns1='urn:oio:sagdok:3.0.0'>
                <ns1:StatusKode>0</ns1:StatusKode>
            </ns1:StandardRetur>
            <ns0:LaesResultat>
                <ns1:MedarbejderRef xmlns:ns1='urn:oio:sagdok:3.0.0'>
                    <ns1:UUIDIdentifikator/>
                    <ns1:URNIdentifikator>URN:OIO:KMD:LPE:MEDARBEJDERNR:00000001</ns1:URNIdentifikator>
                </ns1:MedarbejderRef>
                <ns0:Registrering>
                    <ns0:AttributListe>
                        <ns0:Egenskaber>
                            <ns0:Persondata>
                                <ns0:Gyldighedsstart>1970-12-12</ns0:Gyldighedsstart>
                                <ns0:Gyldighedsstop>2099-01-01</ns0:Gyldighedsstop>
                                <ns0:CPR-nummer>1234567890</ns0:CPR-nummer>
                                <ns0:Efternavn>Doe</ns0:Efternavn>
                                <ns0:Fornavn>John</ns0:Fornavn>
                                <ns0:PersonaleIdentifikator>01</ns0:PersonaleIdentifikator>
                            </ns0:Persondata>
                            <ns0:Persondata>
                                <ns0:Gyldighedsstart>2099-01-01</ns0:Gyldighedsstart>
                                <ns0:Gyldighedsstop>9999-12-31</ns0:Gyldighedsstop>
                                <ns0:CPR-nummer>1234567890</ns0:CPR-nummer>
                                <ns0:Efternavn>Doe</ns0:Efternavn>
                                <ns0:Fornavn>John</ns0:Fornavn>
                                <ns0:PersonaleIdentifikator>01</ns0:PersonaleIdentifikator>
                            </ns0:Persondata>
                        </ns0:Egenskaber>
                    </ns0:AttributListe>
                    <ns0:RelationListe/>
                </ns0:Registrering>
            </ns0:LaesResultat>
            <ns0:LaesResultat>
                <ns1:MedarbejderRef xmlns:ns1='urn:oio:sagdok:3.0.0'>
                    <ns1:UUIDIdentifikator/>
                    <ns1:URNIdentifikator>URN:OIO:KMD:LPE:MEDARBEJDERNR:00000002</ns1:URNIdentifikator>
                </ns1:MedarbejderRef>
                <ns0:Registrering>
                    <ns0:AttributListe>
                        <ns0:Egenskaber>
                            <ns0:Persondata>
                                <ns0:Gyldighedsstart>1970-12-12</ns0:Gyldighedsstart>
                                <ns0:Gyldighedsstop>2099-01-01</ns0:Gyldighedsstop>
                                <ns0:CPR-nummer>0987654321</ns0:CPR-nummer>
                                <ns0:Efternavn>Doe</ns0:Efternavn>
                                <ns0:Fornavn>Jane</ns0:Fornavn>
                                <ns0:PersonaleIdentifikator>01</ns0:PersonaleIdentifikator>
                            </ns0:Persondata>
                        </ns0:Egenskaber>
                    </ns0:AttributListe>
                    <ns0:RelationListe/>
                </ns0:Registrering>
            </ns0:LaesResultat>
        </ns0:ListOutput>
    </SOAP:Body>
</SOAP:Envelope>

The only thing I could think of that might fix it, is adding an "ElementName" to

[System.Xml.Serialization.XmlRootAttribute

But tried it, and it gave same error.

What am I missing ?

Kiesp
  • 76
  • 4
  • did you check the XML file in the XML validator? I doubt, your XML is not valid. – Muhammad Bashir Feb 22 '23 at 09:59
  • every XML has a closing tag (or call it node), in the snapshot, it appears, some closing nodes/tags are missing. – Muhammad Bashir Feb 22 '23 at 10:00
  • 1
    `Exception InvalidOperationException: was not expected.` -- looks like you've got an invisible character at the start. Open it in a hex editor, see what the first few characters are? – canton7 Feb 22 '23 at 10:02
  • 1
    @canton7 Not sure why it removed the text there oO It's supposed to say "InvalidOperationException: was not expected." I'll update the question :) – Kiesp Feb 22 '23 at 10:11
  • My go-to trick for debugging XmlSerializer deserialization is to take your model and *serialize* it to XML. See how the output differs from the thing you're trying to deserialize, then tweak your model until they match. Then deserialization should work – canton7 Feb 22 '23 at 10:13
  • @MuhammadBashir I've tried validating the xml and no errors were found. I'll update my question with all of the xml :) – Kiesp Feb 22 '23 at 10:15
  • @canton7 I'm not sure how I do that, but your comment made me look through all my elements and names and found that I had been trying to deserialize EnvelopeBody rather than Envelope. So thanks! :D – Kiesp Feb 22 '23 at 12:01
  • Just create an `Envelope` object, then use `XmlSerializer.Serialize` on it to generate XML – canton7 Feb 22 '23 at 13:25

1 Answers1

0

Turns out it was because I was trying to deserialize my EnvelopeBody rather than the Envelope. Between the naming in the xml file and the naming in the autogenerated classes, I simply mixed the two of them up, because they were so similar.

Deserialization should always start at the root element

Kiesp
  • 76
  • 4