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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…