如何反序列化肥皂响应? [英] How to deserialize soap response?

查看:24
本文介绍了如何反序列化肥皂响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将以下 xml 反序列化为 c# 对象:

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>

我的课程设置如下:

[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;
    }

但是我没有在我的对象中获得结果值.它只反序列化到 TopupResonse 并且为空.我试过谷歌了一下,但找不到任何具体的东西.我认为,问题出在命名空间上,并不确定.任何帮助将非常感激.

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.

推荐答案

我不得不更新两个类如下:

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.

这篇关于如何反序列化肥皂响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆