Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

c# - How to deserialize soap response?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...