2016-05-05 38 views
0

我我下面的代碼生成一個XML,並返回生成的XML,但目前的格式不是預期的結構:格式問題與類生成的xml在ASP.Net C#

我的班級:

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,但目前的格式不是預期的結構:可是我要的結果與去除標籤0的完全相同:

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

請我想要的標籤完全去除 –

回答

0

從結果XML修改你的類結構如下圖,

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

public class Response { 
    ... 
    public Accounts Accounts { get; set; } 
} 
+0

感謝@ codeninja.sj請我仍然有問題在這裏: –

+0

謝謝@ codeninja.sj請仍然有問題在這裏給空對象的返回值:foreach(XmlElement a in accts) { var acctNo = a.InnerText.Substring(0,10); //accounts.Add(新賬戶{account = acctNo}); accounts.account.Add(acctNo); } –

+0

您試圖字符串值添加到列表中未初始化的對象(即表) –