您好,我正在开发一个Web-API项目,该项目可以连接并使用多设备。我有一个要求,例如直接在移动打印设备(WizarPOS)中打印XML格式,我需要发送如下所示的响应格式。
<RESPONSE TYPE="PRINT" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<TERMINALID>993324</TERMINALID>
<LOCALDATETIME>2018-11-16 09:08:40</LOCALDATETIME>
<SERVERDATETIME>6/29/2018 3:33:34 PM</SERVERDATETIME>
<TXID>880034435</TXID>
<HOSTTXID>ID00008769249</HOSTTXID>
<AMOUNT>500</AMOUNT>
<CURRENCY>634</CURRENCY>
<LIMIT>0</LIMIT>
<RECEIPT>
<LINES>14</LINES>
<LINE>Provider Pin</LINE>
<LINE>TerminalID: 993324</LINE>
<LINE>Date: 29.06.2018</LINE>
<LINE>TimeOfDay: 15:06:SS</LINE>
<LINE>Trace-No: 160537</LINE>
<LINE>Receipt-No: 475514</LINE>
<LINE>--------------------------------------------------</LINE>
<LINE>Value: 500 QAR</LINE>
<LINE>Product without VAT</LINE>
<LINE>Service: 7736737741</LINE>
<LINE>Hotline: 0110/400773</LINE>
<LINE>Serial Number: 778617719</LINE>
<LINE>CashCode:</LINE>
<LINE>2866-8195-3923-8894</LINE>
</RECEIPT>
<RESULT>0</RESULT>
<RESULTTEXT>Transaction Successful</RESULTTEXT>
<PINCREDENTIALS>
<PIN>2846-4607-1987-3562</PIN>
<SERIAL>778617719</SERIAL>
<VALIDTO>11/29/2018 3:33:34 PM</VALIDTO>
</PINCREDENTIALS>
为此,我创建了两个主类和两个嵌套类,其中一个嵌套类具有字符串派生类列表,如下所示
[XmlRoot("RESPONSE", DataType = "PRINT")]
public class PinDirectResponseVM
{
public int TERMINALID { get; set; }
public string LOCALDATETIME { get; set; }
public string SERVERDATETIME { get; set; }
public int TXID { get; set; }
public string HOSTTXID { get; set; }
public string AMOUNT { get; set; }
public string CURRENCY { get; set; }
public string LIMIT { get; set; }
[XmlArrayItem(ElementName = "LINE")]
public ReceiptResponseVM RECEIPT { get; set; }
public string RESULT { get; set; }
public string RESULTTEXT { get; set; }
public string AID { get; set; }
public PinCredentialsResponseVM PINCREDENTIALS { get; set; }
}
public class ReceiptResponseVM : List<string>
{
public int LINES { get; set; }
}
public class PinCredentialsResponseVM
{
public string PIN { get; set; }
public string SERIAL { get; set; }
public string VALIDTO { get; set; }
}
当我返回'PinDirectResponseVM'对象时没有得到<LINES>14<LINES>
标签时,我得到了这样的响应
<RESPONSE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<TERMINALID>993324</TERMINALID>
<LOCALDATETIME>2018-11-16 09:08:40</LOCALDATETIME>
<SERVERDATETIME>6/29/2018 3:33:34 PM</SERVERDATETIME>
<TXID>880034435</TXID>
<HOSTTXID>ID00008769249</HOSTTXID>
<AMOUNT>500</AMOUNT>
<CURRENCY>634</CURRENCY>
<LIMIT>0</LIMIT>
<RECEIPT>
<LINE>Provider Pin</LINE>
<LINE>TerminalID: 993324</LINE>
<LINE>Date: 29.06.2018</LINE>
<LINE>TimeOfDay: 15:06:SS</LINE>
<LINE>Trace-No: 160537</LINE>
<LINE>Receipt-No: 475514</LINE>
<LINE>--------------------------------------------------</LINE>
<LINE>Value: 500 QAR</LINE>
<LINE>Product without VAT</LINE>
<LINE>Service: 7736737741</LINE>
<LINE>Hotline: 0110/400773</LINE>
<LINE>Serial Number: 778617719</LINE>
<LINE>CashCode:</LINE>
<LINE>2866-8195-3923-8894</LINE>
</RECEIPT>
<RESULT>0</RESULT>
<RESULTTEXT>Transaction Successful</RESULTTEXT>
<PINCREDENTIALS>
<PIN>2846-4607-1987-3562</PIN>
<SERIAL>778617719</SERIAL>
<VALIDTO>11/29/2018 3:33:34 PM</VALIDTO>
</PINCREDENTIALS>
因此,请根据我的要求帮助我获得响应,为此生成XML的最佳方法是什么?
答案 0 :(得分:1)
这是因为<RECEIPT></RECEIPT>
节点中有两个不同的子节点
<LINES></LINES>
<LINE></LINE>
您可以执行以下操作:
XmlRoot("RESPONSE", DataType = "PRINT")]
public class PinDirectResponseVM
{
public int TERMINALID { get; set; }
public string LOCALDATETIME { get; set; }
public string SERVERDATETIME { get; set; }
public int TXID { get; set; }
public string HOSTTXID { get; set; }
public string AMOUNT { get; set; }
public string CURRENCY { get; set; }
public string LIMIT { get; set; }
public ReceiptResponseVM RECEIPT { get; set; }
public string RESULT { get; set; }
public string RESULTTEXT { get; set; }
public string AID { get; set; }
public PinCredentialsResponseVM PINCREDENTIALS { get; set; }
}
public class ReceiptResponseVM //: List<string>
{
[XmlElement(Order = 1, ElementName = "LINES")]
public int LINES { get; set; }
[XmlElement(Order = 2, ElementName = "LINE")]
public List<string> LINE {get; set;}
}
public class PinCredentialsResponseVM
{
public string PIN { get; set; }
public string SERIAL { get; set; }
public string VALIDTO { get; set; }
}