How to deserialize soap response?
Asked Answered
P

3

9

I'm trying to deserialize following xml to c# object:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
  <ns2:topupResponse xmlns:ns2="http://www.claro.com.hn/esb/ws/topups/" 
 xmlns:ns3="http://www.claro.com.hn/esb/ws/ops/">
     <result>
        <conversionRate>7.7765</conversionRate>
        <datetime>2018-01-10T08:33:19.685-06:00</datetime>
        <distAmount>5.00</distAmount>
        <msisdn>50279613880</msisdn>
        <newBalance>38</newBalance>
        <operAmount>38.882500</operAmount>
        <responseCode>0</responseCode>
        <transId>228855</transId>
     </result>
  </ns2:topupResponse>

I've setup my classes like this:

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

public class ResponseBody
{
    [XmlElement(ElementName = "topupResponse", Namespace = "http://www.claro.com.hn/esb/ws/topups/, http://www.claro.com.hn/esb/ws/ops/")]
    public TopupResponse topupResponse { get; set; }
}

public class TopupResponse
{
    public Result result { get; set; }
}

public class Result
{
    public string conversionRate { get; set; }
    public string datetime { get; set; }
    public string distAmount { get; set; }
    public string msisdn { get; set; }
    public string newBalance { get; set; }
    public string operAmount { get; set; }
    public string responseCode { get; set; }
    public string transId { get; set; }
}

I'm using following method to deserialize:

  private static object XmlDeserializeFromString(string objectData, Type type)
    {
        objectData = RemoveInvalidXmlCharacters(objectData);//customizeoption if you know the xml you receive wont have any invalid characters you can remove this function

        var serializer = new XmlSerializer(type);
        object result;
        using (var reader = new StringReader(objectData))
        {
            result = serializer.Deserialize(reader);
        }
        return result;
    }

But I'm not getting result values in my object. It only deserializing upto TopupResonse and that's null. I've tried Google a bit but couldn't find anything concrete. I think, the issue is with the namespaces, not exactly sure. Any help would be really appreciated.

Principalities answered 7/2, 2018 at 10:24 Comment(2)
First try to serialize sample class data which is easier to debug. Once the serialize data matches you actual xml then you will be able to easily deserialize the data.Farther
Good idea! Will try that. Thanks.Principalities
P
11

I had to update two class as follows:

 public class TopupResponse
{
    [XmlElement(Namespace = "")]
    public Result result { get; set; }
}

public class Result
{
    [XmlElement(Namespace = "")]
    public string conversionRate { get; set; }

    [XmlElement(Namespace = "")]
    public string datetime { get; set; }

    [XmlElement(Namespace = "")]
    public string distAmount { get; set; }

    [XmlElement(Namespace = "")]
    public string msisdn { get; set; }

    [XmlElement(Namespace = "")]
    public string newBalance { get; set; }

    [XmlElement(Namespace = "")]
    public string operAmount { get; set; }

    [XmlElement(Namespace = "")]
    public string responseCode { get; set; }

    [XmlElement(Namespace = "")]
    public string transId { get; set; }
}

Adding empty namespace to result class solved the issue. sharing, just in case if anyone finds it helpful.

Principalities answered 7/2, 2018 at 11:43 Comment(1)
How was you calling the method: XmlDeserializeFromString()?Virgo
P
6

Alternatively, we can use Visual Studio's Paste Special feature to create c# class from xml or json, which is very easy and efficient.

Principalities answered 9/2, 2018 at 5:27 Comment(0)
G
0
private static object XmlDeserializeFromString(string objectData, Type type)
{
    objectData = RemoveInvalidXmlCharacters(objectData);//customizeoption if you know the xml you receive wont have any invalid characters you can remove this function

    var serializer = new XmlSerializer(type);
    object result;
    using (var reader = new StringReader(objectData))
    {
        result = serializer.Deserialize(reader);
    }
    return result;
}

Can I know what was the type you used in the above function? What did you pass for type? Objectdata would be xml I believe.

Gorski answered 17/7, 2020 at 7:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.