简体   繁体   English

使用派生嵌套类的字符串列表将类对象序列化为C#Webapi中的XML

[英]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. 嗨,我正在开发Web-API项目,该项目可以连接并使用多设备。 I have one requirement like print XML format directly in mobile print device(WizarPOS), i need to send response format as given below. 我有一个要求,例如直接在移动打印设备(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>

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 当我返回'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>

So please help me to get the response as per my requirement, what's the best way to generate XML for it? 因此,请帮助我根据我的要求获得响应,为此生成XML的最佳方法是什么?

This is because there are two different child nodes in <RECEIPT></RECEIPT> node which are 这是因为<RECEIPT></RECEIPT>节点中有两个不同的子节点,分别是

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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