xml - Deserialization not filling data - C# -
i trying deserialize xml . sample xml given below
<?xml version="1.0" ?> <transaction_response> <transaction> <transaction_id>25429</transaction_id> <merchant_acc_no>02700701354375000964</merchant_acc_no> <txn_status>f</txn_status> <txn_signature>a16af68d4c3e2280e44bd7c2c23f2af6cb1f0e5a28c266ea741608e72b1a5e4224da5b975909cc43c53b6c0f7f1bbf0820269caa3e350dd1812484edc499b279</txn_signature> <txn_signature2>b1684258ea112c8b5ba51f73cda9864d1bb98e04f5a78b67a3e539bef96ccf4d16cff6b9e04818b50e855e0783bb075309d112ca596bdc49f9738c4bf3aa1fb4</txn_signature2> <tran_date>29-09-2015 07:36:59</tran_date> <merchant_tranid>150929093703rudzmx4</merchant_tranid> <response_code>9967</response_code> <response_desc>bank rejected transaction!</response_desc> <customer_id>rudzmx</customer_id> <auth_id /> <auth_date /> <capture_date /> <sales_date /> <void_rev_date /> <refund_date /> <refund_amount>0.00</refund_amount> </transaction> </transaction_response>
following class
[xmltype("transaction_response")] public class bankqueryresponse { [xmlelement("transaction_id")] public string transactionid { get; set; } [xmlelement("merchant_acc_no")] public string merchantaccno { get; set; } [xmlelement("txn_signature")] public string txnsignature { get; set; } [xmlelement("tran_date")] public datetime trandate { get; set; } [xmlelement("txn_status")] public string txnstatus { get; set; } [xmlelement("refund_date")] public datetime refunddate { get; set; } [xmlelement("response_code")] public string responsecode { get; set; } [xmlelement("response_desc")] public string responsedesc { get; set; } [xmlattribute("merchant_tranid")] public string merchanttranid { get; set; } }
the deserialization code
bankqueryresponse result = new bankqueryresponse(); if(!string.isnullorempty(responsedata)) { xmlserializer serializer = new xmlserializer(typeof(bankqueryresponse)); using(textreader xmlreader = new stringreader(responsedata)) { result = (bankqueryresponse) serializer.deserialize(xmlreader); } }
i getting value in result null . not sure whats reason . can 1 throws light issue. missing while deserialising
you should modify code this
[xmltype("transaction_response")] public class transactionresponse { [xmlelement("transaction")] public bankqueryresponse response { get; set; } }
this change
public class bankqueryresponse { [xmlelement("transaction_id")] public string transactionid { get; set; } [xmlelement("merchant_acc_no")] public string merchantaccno { get; set; } [xmlelement("txn_signature")] public string txnsignature { get; set; } [xmlelement("tran_date")] public datetime trandate { get; set; } [xmlelement("txn_status")] public string txnstatus { get; set; } [xmlelement("refund_date")] public datetime refunddate { get; set; } [xmlelement("response_code")] public string responsecode { get; set; } [xmlelement("response_desc")] public string responsedesc { get; set; } [xmlattribute("merchant_tranid")] public string merchanttranid { get; set; } }
deseralization code
transactionresponse result = new transactionresponse(); if(!string.isnullorempty(responsedata)) { xmlserializer serializer = new xmlserializer(typeof(transactionresponse)); using(textreader xmlreader = new stringreader(responsedata)) { result = (transactionresponse) serializer.deserialize(xmlreader); } }
Comments
Post a Comment