I am parsing the following line of code via a specific format. Line is:
S|111111|87654321|Bar UK|BCreace UK|GBP|24/08/2010|
The Format is:
Index Field Length S0 - 1 S1 - 6 S2 - 34 .... ... S6 - 10
I am validating using many if statements. Could anyone suggest a better approach?
private static StatementLineResult Validate(string delimitedRecord) { if (delimitedRecord == null) throw new ArgumentNullException("delimitedRecord"); var items = delimitedRecord.Split('|'); if(items.Length != 19) throw new Exception("Improper line format"); var errorMessage = new Dictionary<string, string>(); var bankIdentifierCodes = new List<string> {"ABCDGB2L", "EFGHGB2L"}; if (items[0].Length != 1 || items[0] != "S") errorMessage.Add("Record Identifier","Invalid Record Identifier"); var sortCode = 0; if (!int.TryParse(items[1], out sortCode) || items[1].Length > 6) errorMessage.Add("Sort Code", "Invalid sort code"); if (string.IsNullOrEmpty(items[2]) || items[1].Length > 34) errorMessage.Add("Account Number", "Invalid account number"); if (string.IsNullOrEmpty(items[3]) || items[1].Length > 35) errorMessage.Add("Account Alias", "Invalid account alias"); if (string.IsNullOrEmpty(items[4]) || items[1].Length > 35) errorMessage.Add("Account Name", "Invalid account name"); if(string.IsNullOrEmpty(items[5]) || items[5].Length != 3) errorMessage.Add("Account Currency", "Invalid account currency"); if (!string.IsNullOrEmpty(items[6]) && items[6].Length > 20) errorMessage.Add("Account Type", "Invalid account type"); if(string.IsNullOrEmpty(items[7]) || items[7].Length != 8 || !bankIdentifierCodes.Contains(items[7],StringComparer.OrdinalIgnoreCase)) errorMessage.Add("Bank Identifier Code", "Invalid bank identifier code"); if (!string.IsNullOrEmpty(items[8]) && items[8].Length > 35) errorMessage.Add("Bank Name", "Invalid bank name"); if (!string.IsNullOrEmpty(items[9]) && items[9].Length > 27) errorMessage.Add("Bank Branch Name", "Invalid bank branch name"); DateTime transactionDate; if (!DateTime.TryParse(items[10], out transactionDate)) errorMessage.Add("Transaction Date", "Invalid date"); if (!string.IsNullOrEmpty(items[11]) && items[11].Length > 25) errorMessage.Add("Narrative Line 1", "Invalid narrative line 1"); if (!string.IsNullOrEmpty(items[12]) && items[12].Length > 25) errorMessage.Add("Narrative Line 2", "Invalid narrative line 2"); if (!string.IsNullOrEmpty(items[13]) && items[13].Length > 25) errorMessage.Add("Narrative Line 13", "Invalid narrative line 13"); if (!string.IsNullOrEmpty(items[14]) && items[14].Length > 25) errorMessage.Add("Narrative Line 14", "Invalid narrative line 14"); if (!string.IsNullOrEmpty(items[15]) && items[15].Length > 25) errorMessage.Add("Narrative Line 15", "Invalid narrative line 15"); if(_transactionTypes.First(item=>item.TransactionType==items[16]) == null) errorMessage.Add("Transaction Type", "Invalid transaction type"); decimal debitValue; if(items[17] != "" && !Decimal.TryParse(items[17], out debitValue)) errorMessage.Add("Debit Value", "Invalid debit amount"); decimal creditValue; if (items[18] != "" && !Decimal.TryParse(items[18], out creditValue)) errorMessage.Add("Credit Value", "Invalid credit amount"); return new StatementLineResult() { ErrorMessages = errorMessage, Data = BankLineData(delimitedRecord), IsValid = errorMessage.Count==0 }; }