反序列化不填充数据 - C#
Posted
技术标签:
【中文标题】反序列化不填充数据 - C#【英文标题】:Deserialization not filling data - C# 【发布时间】:2015-12-27 15:47:58 【问题描述】:我正在尝试反序列化 XML 。 示例 XML 如下所示
<?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>
下面是类
[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;
反序列化代码为
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);
我将结果中的所有值都设为 null 。我不确定是什么原因。有人可以解决这个问题。我在反序列化时是否遗漏了什么
【问题讨论】:
TRANSACTION
级别丢失了。
【参考方案1】:
你应该像这样修改代码
[XmlType("TRANSACTION_RESPONSE")]
public class TransactionResponse
[XmlElement("TRANSACTION")]
public BankQueryResponse Response get; set;
会变成这样的
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;
反序列化代码是这样的
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);
【讨论】:
谢谢@Mohit。我错过了反序列化中的一级层次结构。你的建议奏效了。 很高兴为您提供帮助:)以上是关于反序列化不填充数据 - C#的主要内容,如果未能解决你的问题,请参考以下文章