【问题标题】:How to deserialize soap response?如何反序列化肥皂响应?
【发布时间】:2018-07-17 14:21:07
【问题描述】:

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

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

我正在使用以下方法进行反序列化:

  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 并且为空。我已经尝试了谷歌,但找不到任何具体的东西。我认为,问题出在命名空间上,并不完全确定。任何帮助将非常感激。

【问题讨论】:

  • 先尝试序列化样本类数据,这样更容易调试。一旦序列化数据与您的实际 xml 匹配,您就可以轻松地反序列化数据。
  • 好主意!会尝试的。谢谢。

标签: c# xml wcf soap xml-deserialization


【解决方案1】:

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

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

向结果类添加空命名空间解决了这个问题。分享,以防万一有人觉得有用。

【讨论】:

    【解决方案2】:

    或者,我们可以使用 Visual Studio 的选择性粘贴功能从 xml 或 json 创建 c# 类,非常简单高效。

    【讨论】:

      【解决方案3】:
      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;
      }
      

      我能知道您在上述函数中使用的类型是什么吗?你通过什么类型的?我相信 Objectdata 会是 xml。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-11
        • 2017-09-30
        • 2023-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-05
        相关资源
        最近更新 更多