0
votes

I need to deserialize this xml

<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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">
    <SOAP-ENV:Body>
        <ns1:estraiListaAttiFascicoloResponse xmlns:ns1="urn:BEAFascicoloInformatico-distr">
            <return xmlns:ns2="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:Array" ns2:arrayType="ns1:BEADocumentoFascicoloVO[2]">
                <item xsi:type="ns1:BEADocumentoFascicoloVO">
                    <annoFascicolo xsi:type="xsd:string">1998</annoFascicolo>
                </item>
                <item xsi:type="ns1:BEADocumentoFascicoloVO">
                    <annoFascicolo xsi:type="xsd:string">1998</annoFascicolo>
                </item>
            </return>
        </ns1:estraiListaAttiFascicoloResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

i have tried with this classes:

[XmlType(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[XmlRoot(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
public class Envelope
{
    public EnvelopeBody Body { get; set; }

    [XmlAttribute(Form = XmlSchemaForm.Qualified)]
    public string encodingStyle { get; set; }
}

[XmlType(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class EnvelopeBody
{
    [XmlElement(Namespace = "urn:BEAFascicoloInformatico-distr")]
    public estraiListaAttiFascicoloResponse estraiListaAttiFascicoloResponse { get; set; }
}

/// <remarks/>
[XmlType(AnonymousType = true, Namespace = "urn:BEAFascicoloInformatico-distr")]
[XmlRoot(Namespace = "urn:BEAFascicoloInformatico-distr", IsNullable = false)]
public class estraiListaAttiFascicoloResponse
{
    [XmlElement(Namespace = "")]
    public @return @return { get; set; }
}

[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public class @return
{
    [XmlElement("item")]
    public returnItem[] item { get; set; }

    [XmlAttribute(Form = XmlSchemaForm.Qualified, Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
    public string arrayType { get; set; }
}

[XmlType(AnonymousType = true)]
public class returnItem
{
    public ushort annoFascicolo { get; set; }
}

and this code

var serializer = new XmlSerializer(typeof(Envelope));

var sw = new StringReader(xmlText);

using (var xmlTextReader = XmlReader.Create(sw))
{
   var converted = serializer.Deserialize(xmlTextReader);
}

but i receive this error at runtime

Additional information: There is an error in XML document (5, 5).

and the inner exception in this

The specified type was not recognized: name='Array', Namespace='http://schemas.xmlsoap.org/soap/encoding/', at .

but if i add the namespace "http://schemas.xmlsoap.org/soap/encoding/" to @return property of estraiListaAttiFascicoloResponse class the exception disappears but don't deserialize array items anyway

Can someone help me?

Thanks

1
If you have the xsd you can use xsd2code to generate your source classes so you don't have to manually do it. The tools are usually pretty good at working with the more nuancsed aspects of XML that are difficult to do by hand - Philip Pittle
I have used that tool for generate the classes for deserialize xml but they don't work. The posted code is obtained with "Paste Xml as class" function of VS but, as above, they don't work - Achille
Than you might have found a bug in the class generator! - Philip Pittle
I think the same. But now i need to correct manually the generated classes. - Achille

1 Answers

0
votes

I do not see a resolve for namespace ns1.

You could also check xml validity using the great free and open source tool XML Notepad, to be downloaded from Microsoft.