Read whole file in blocks in c#

848 Views Asked by At

I have a file from bank that is structure in very special way. Where there is account number (25), account balance start (60F), account balance stop (62F), and transactions (61 for transaction and 86 for this transaction details).

:20:STARTSUM
:25:/PL2321109943011
:28C:0330/001
:60F:C100PLN38,74
:62F:C103PLN38,74
-


:20:STARTSUM
:25:/PL24160011982002123456001
:28C:0403/001
:60F:C030403PLN36000,00
:61:0304030403CN100,00S723NONREF//CENT30403H000200
:86:723>00PRZELEW OTRZYMANY ELIXIR>20Fakt VAT 1 nr 00911/03
:86:723>3010501445>3125-00001201467436
:86:723>32Firma XXXXXXXXXXă>33Krakow
:61: 0304030403DN1000,00S223NONREF//CENT30403H002342
:86:223>00PRZEL KRAJ MULTICASH>20000004020 20021224 Fa. 0095
:86:223>21007203-FIRMA SP. Z O>308510101010>311234567890123456
:86:223>32FRIMA XXXXXXXXXX UL. GNI>33EZNIENSKA 1
:86:223>3885101010101234567890123456
:86:223>6085101010101234567890123456
:61:0304030403CN100,00S723NONREF//CENT30403H000230
:86:723>00PRZELEW OTRZYMANY ELIXIR>20Fakt VAT 1 nr 00911/03
:86:723>308510101010>311234567890123456
:86:723>32Firma XXXXXXXXXXă>33Krakow
:86:223>3885101010101234567890123456
:86:223>6085101010101234567890123456
:62F:C030403PLN35200,00
-


:20:STARTSUM
:25:/PL2321109944011
:28C:0330/001
:60F:C120330PLN43,45
:62F:C120330PLN43,45
-


:20:STARTSUM
:25:/PL1109945011
:28C:0330/001
:60F:C1230PLN3,50
:62F:C1230PLN3,50
-

It always has 2 lines of breaks between each block. I would like to put those blocks into an object that I create.

string[] test = File.ReadAllLines(file);
foreach (var s in test) {

}

How can I approach it the proper way? Normally I would go by foreach line and try to split the blocks by empty 2 lines then go further by doing multiple if/else statements. But maybe there's simple/better approach to this?

2

There are 2 best solutions below

6
On BEST ANSWER
string[] blocks = (file.ReadAllText(file)).split(new string[] {"\n\n\n"}, StringSplitOptions.None)

Should break it into blocks for you.

0
On

Look at using StreamReader and use your logic to find what you are looking for.

StreamReader _sr = new StreamReader("YourFile");
_sr.ReadLine();