简体   繁体   中英

Formatting issue with a class generated xml in ASP.Net C#

My below my code which generate an xml and return the generated xml but the current format is not the structure expected:

My Class:

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

Result:

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

My above code generate an xml and return the generated xml but the current format is not the structure expected:But I want the result to be exactly with the removal of tag <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>

Modify your class structure as below,

public class Accounts { 
   public Accounts ()
   {
      Account = new List<string>();
   }
   public List<string> Account { get; set; }
}

public class Response {
   ...
   public Accounts Accounts { 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