Home > Mobile >  Reading an MT940 file data and storing into database table
Reading an MT940 file data and storing into database table

Time:09-29

I am new to MT940 file and I am searching for a sample code to read content of .mt940 and store in a database table with respective fields. I am struggling to analyze it. Is there any simple way to parse and save it in a table?

For example take the below line(this is not entire mt940 just a single line)

:61:2009230923D4086,74NDDTNONREF //NONREF

How can I retrieve customer reference(=NONREF ) from the above line? It does not present in same index for all lines. Some times it starts from 28index and some times 30 index.

CodePudding user response:

Use Regex

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace ConsoleApp1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        const string pattern = @":61:(?'ValueDate'.{6})(?'EntryDate'.{4})(?'Mark'.{2})(?'FundCode'.{1})(?'Amount'[\d,] )(?'ID'.{4})((?'CustomerReference'.{1,16})(//)(?'BankReference'.*)|(?'CustomerReference'.{1,16}))";
        static void Main(string[] args)
        {
            string data = File.ReadAllText(FILENAME);
            MatchCollection matches = Regex.Matches(data, pattern, RegexOptions.Multiline);

            foreach (Match match in matches)
            {

                string valueDate = match.Groups["ValueDate"].Value;
                string entryDate = match.Groups["EntryDate"].Value;
                string mark = match.Groups["Mark"].Value;
                string fundCode = match.Groups["FundCode"].Value;
                string amount = match.Groups["Amount"].Value;
                string id = match.Groups["ID"].Value;
                string customerReference = match.Groups["CustomerReference"].Value;
                string bankReference = match.Groups["BankReference"].Value;
                Console.WriteLine("ValueDate = {0}, EntryDate = {1}, Mark = {2}, FundCode = {3}, Amount = {4}, ID = {5}, Customer = {6}, Bank = {7}",
                    valueDate, entryDate, mark, fundCode, amount, id, customerReference, bankReference);
            }
            Console.ReadLine();

        }
    }
}
  • Related