1

I have problems deserializing this XML.

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns:getBasketResponse xmlns:ns="http://capi.basket.webservice.wg_base_app.enfinity.wg.com">
            <ns:return xmlns:ax27="http://capi.webservice.wg_base_app.enfinity.wg.com/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax27:Response">
                <ax27:costs xsi:type="ax27:Cost">
                    <ax27:name>Verwerking (verpakking &amp; levering)</ax27:name>
                </ax27:costs>
            </ns:return>
        </ns:getBasketResponse>
    </soapenv:Body>
</soapenv:Envelope>

These are my classes:

    public class GetBasketResponse
    {
        [XmlElement("ns:return")] public ReturnData ReturnData { get; set; }
    }
    public class ReturnData
    {
        [XmlElement("ax27:costs", Namespace= "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")] 
        public List<Cost> Costs { get; set; }
    }
    public class Cost
    {
        [XmlElement("ax27:name", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
        public string Name { get; set; }
    }

This is my code to deserialize:

    XmlSerializer serializer = new XmlSerializer(typeof(GetBasketResponse));
    using (StringReader reader = new StringReader(xml))
    {
            GetBasketResponse result = (GetBasketResponse)serializer.Deserialize(reader);
    }

Error: InvalidOperationException: The specified type was not recognized: name='Response', namespace='http://capi.webservice.wg_base_app.enfinity.wg.com/xsd', at <return xmlns='http://capi.basket.webservice.wg_base_app.enfinity.wg.com'>.

2 Answers2

1

You should use a tool to generate the correct class structure for your XML. For example, this one

For the provided XML, a tool generates this class structure

[XmlRoot(ElementName="costs", Namespace="http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
public class Costs { 

    [XmlElement(ElementName="name", Namespace="http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")] 
    public string Name; 

    [XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")] 
    public string Type; 

    [XmlText] 
    public string Text; 
}

[XmlRoot(ElementName="return", Namespace="http://capi.basket.webservice.wg_base_app.enfinity.wg.com")]
public class Return { 

    [XmlElement(ElementName="costs", Namespace="http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")] 
    public Costs Costs; 

    [XmlAttribute(AttributeName="ax27", Namespace="http://www.w3.org/2000/xmlns/")] 
    public string Ax27; 

    [XmlAttribute(AttributeName="xsi", Namespace="http://www.w3.org/2000/xmlns/")] 
    public string Xsi; 

    [XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")] 
    public string Type; 

    [XmlText] 
    public string Text; 
}

[XmlRoot(ElementName="getBasketResponse", Namespace="http://capi.basket.webservice.wg_base_app.enfinity.wg.com")]
public class GetBasketResponse { 

    [XmlElement(ElementName="return", Namespace="http://capi.basket.webservice.wg_base_app.enfinity.wg.com")] 
    public Return Return; 

    [XmlAttribute(AttributeName="ns", Namespace="http://www.w3.org/2000/xmlns/")] 
    public string Ns; 

    [XmlText] 
    public string Text; 
}

[XmlRoot(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Body { 

    [XmlElement(ElementName="getBasketResponse", Namespace="http://capi.basket.webservice.wg_base_app.enfinity.wg.com")] 
    public GetBasketResponse GetBasketResponse; 
}

[XmlRoot(ElementName="Envelope", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope { 

    [XmlElement(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")] 
    public Body Body; 

    [XmlAttribute(AttributeName="soapenv", Namespace="http://www.w3.org/2000/xmlns/")] 
    public string Soapenv; 

    [XmlText] 
    public string Text; 
}

Then you can use it like this

using System.Xml.Serialization;
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));

using (StringReader reader = new StringReader(xml))
{
    var test = (Envelope)serializer.Deserialize(reader);
}
Hunter Tran
  • 13,257
  • 2
  • 14
  • 23
  • Thank you for the tip. But It doesn't work. I get this error InvalidOperationException: was not expected. – Benjamin Goyvaerts Jun 15 '23 at 07:37
  • I believe your XML has more items in it. Have a look at this answer to see if it can help: https://stackoverflow.com/questions/52775320/c-sharp-cant-deserialize-xml-containing-xsitype – Hunter Tran Jun 15 '23 at 13:53
0

U can refine ur classes like below

[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
    [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public Body Body { get; set; }
}

public class Body
{
    [XmlElement(ElementName = "getBasketResponse", Namespace = "http://capi.basket.webservice.wg_base_app.enfinity.wg.com")]
    public GetBasketResponse GetBasketResponse { get; set; }
}

public class GetBasketResponse
{
    [XmlElement(ElementName = "return", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
    public Response Response { get; set; }
}

[XmlRoot(ElementName = "Response", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
public class Response
{
    [XmlElement(ElementName = "costs", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
    public Cost Costs { get; set; }
}

public class Cost
{
    [XmlElement(ElementName = "name", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
    public string Name { get; set; }
}

Then try to use below code to get the job done

XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
using (StringReader reader = new StringReader(xml))
{
    Envelope envelope = (Envelope)serializer.Deserialize(reader);
    GetBasketResponse result = envelope.Body.GetBasketResponse;
}