How to deserialize soap response?

ⅰ亾dé卋堺 提交于 2020-01-25 01:49:46

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/48661345/how-to-deserialize-soap-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!