Search code examples
c#mt940

Reading swift message MT940


Am reading Swift message MT940, it reads fine, until I encounter a slight issue. Below is the snippet of the message am reading;

:61:140423C207430136,99S103R802316/103//3242612
3000024426
:86:/CODETYPTR/027
CBNINGLA
DBLNNGLA
SETL/1404230804+0000
:61:140423C50000000,S202TRONGNCASHSWAP1C//3242830
3000024426
:86:/CODETYPTR/001
FCMBNGLA
NONREF
SETL/1404230918+0000

and this is the code I use to read this line;

if (line.StartsWith(":61:"))
    {
     string strdateE = line.Substring(4, 6);
     string dt = strdateE.Substring(0, 2) + "/" + strdateE.Substring(2, 2) + "/" + strdateE.Substring(4, 2);

inflow940.VALUE_DATE = DateTime.ParseExact(dt, "yy/MM/dd", null);
inflow940.DR_CR = line.Substring(10, 1);int sIndex = line.IndexOf('S');
inflow940.AMOUNT = decimal.Parse(line.Substring(11, sIndex - 11).Replace(',', '.'));
inflow940.TRXNTYPE_IDCODE = line.Substring(line.IndexOf('S'), 4);

     string refr = line.Split(new string[] { "//" }, StringSplitOptions.None)[0];

     string reft = "";

     if (refr.Contains("S202"))
         {
             reft = refr.Replace("S202", "//");
         }
     if (refr.Contains("S103"))
         {
             reft = refr.Replace("S103", "//");
         }
     if (refr.Contains("S102"))
         {
             reft = refr.Replace("S102", "//");
         }
     if (refr.Contains("FTRF"))
         {
             reft = refr.Replace("FTRF", "//");
         }

inflow940.REFERENCE = reft.Split(new string[] { "//" }, StringSplitOptions.None)[1];
         pointer = 2;
         }

The code above works well, but after deploying the solution, I noticed the file am reading from can come in this format

:61:140423C7000000000,FTRFNONREF//3242445
3000024426

My Challenge: Majority of the messages always come with either S202, or S103 or S102 embedded within the line, a situation were I encounter something contrary, like with this message below (FTRF), how can I modify my code to handle that?


Solution

  • The stackoverflow user Jaco has built an open source library SharpMt940Lib. With this library you can parse a mt940 file. I would suggest to test your file with this library. I think, this makes it a lot easier.