简体   繁体   中英

Serialize class object with list of strings derived nested class into XML in c# Webapi

Hi I'm working on Developing a Web-API project, which connects and working with multi-devices. I have one requirement like print XML format directly in mobile print device(WizarPOS), i need to send response format as given below.

<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>

For this i have created two one main class and two nested class, one nested class with list of string derived class as shown below

[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; }
}

When i returning 'PinDirectResponseVM' object not getting <LINES>14<LINES> tag,i getting response like this

<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>

So please help me to get the response as per my requirement, what's the best way to generate XML for it?

This is because there are two different child nodes in <RECEIPT></RECEIPT> node which are

<LINES></LINES>
<LINE></LINE>

You can do something like this:

    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; }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM