在ASP.Net C中使用类生成的xml格式化问题#

时间:2016-05-05 11:55:40

标签: c# asp.net xml

我的代码下面生成一个xml并返回生成的xml,但当前格式不是预期的结构:

我的班级:

public class response
    {
        [StringLength(64)]
        public string reference { get; set; }

        public int responseCode { get; set; }

        [StringLength(140)]
        public string responseMessage { get; set; }

        [StringLength(32)]
        public string transactionId { get; set; }

        public List<account> accounts { get; set; }

    }



    public class account
    {
        [StringLength(64)]
        public string account_number { get; set; }

    }


 rs = "<?xml version='1.0' encoding='UTF-8'?><USSDResponse><Status>true</Status><StatusMessage>Account details returned for 08069262257</StatusMessage><SessionID>31853F5C-A1C1-2A6F-E054-8E1F65C33B15</SessionID><AccountNumber><AccountNo>0003893369</AccountNo><AccountStatus>ACCOUNT OPEN REGULAR</AccountStatus><AvailableBalance>17674.69</AvailableBalance><AccountName>IYEKE IKECHUKWU I.</AccountName><AccountCurrency>NGN</AccountCurrency><ProductName>CURRENT STAFF</ProductName></AccountNumber><AccountNumber><AccountNo>0064612613</AccountNo><AccountStatus>ACCOUNT OPEN REGULAR</AccountStatus><AvailableBalance>201132.18</AvailableBalance><AccountName>IKECHUKWU ISRAEL IYEKE</AccountName><AccountCurrency>NGN</AccountCurrency><ProductName>HIDA</ProductName></AccountNumber></USSDResponse>";

                x.LoadXml(rs);
                status = x.GetElementsByTagName("Status")[0].InnerText;
                SessionID = x.GetElementsByTagName("SessionID")[0].InnerText;


                if (status != null && status == "true")
                {
                    var accts = x.GetElementsByTagName("AccountNo");

                    var names = x.GetElementsByTagName("ProductName");

                    if (accts.Count >= 2)
                    {

                        //var AcctNo = new accounts();
                        foreach (XmlElement a in accts)
                        {
                            var acctNo = a.InnerText.Substring(0, 10);
                            accounts.Add(new account { account_number =  acctNo });


                        }




                        o.accounts = accounts;
                        o.reference = reference;
                        o.responseCode = 6;
                        o.responseMessage = "Please you can only purchase airtime in naira only and no kobo inclusive.";
                        o.transactionId = "Nil";
                        logger.Info($"Wrong amount: {amountString} including kobo entered by the user for mobile number: {msisdn}");
                        return o;
                    }
                }

结果:

<response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <reference>565695769-8490890112091</reference>
    <responseCode>6</responseCode>
    <responseMessage>Please you can only purchase airtime in naira only and no kobo inclusive.</responseMessage>
    <transactionId>Nil</transactionId>
    <accounts>
    <account><account_number>0003893369</account_number></account>
    <account><account_number>0064612613</account_number></account>
    </accounts>
    </response>

我上面的代码生成了一个xml并返回生成的xml但当前格式不是预期的结构:但我希望结果与删除标记<account_number></account_number>完全一致:

    <response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <reference>565695769-8490890112091</reference>
    <responseCode>6</responseCode>
    <responseMessage>Please you can only purchase airtime in naira only and no kobo inclusive.</responseMessage>
    <transactionId>Nil</transactionId>
    <accounts>
    <account>0003893369</account>
    <account>0064612613</account>
    </accounts>
</response>

1 个答案:

答案 0 :(得分:0)

修改您的类结构,如下所示

1,2..