4

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?

aff
  • 162
  • 2
  • 6
  • 17
MadBoy
  • 10,824
  • 24
  • 95
  • 156
  • Some of the answers to [this question](http://stackoverflow.com/questions/1562021/filereader-class-in-c-sharp) will get you close. – Dan Fitch Apr 03 '12 at 20:23
  • You may want to look at this post http://stackoverflow.com/questions/5880401/swift-message-parsing-for-net – Darryl Braaten Apr 03 '12 at 20:23
  • Ahhh, I just love these legacy formats. Even better when the lines aren't delimited but contain fixed-width fields in some arbitrary encoding... – AKX Apr 03 '12 at 20:25

2 Answers2

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

Should break it into blocks for you.

ClassicThunder
  • 1,896
  • 16
  • 25
  • Ooooh, very good! Then you just `blocks.Select(b->b.Split("\n")` and get the blocks as string arrays. – zmbq Apr 03 '12 at 20:25
  • I like this approach, assuming that the file isn't massive as Tudor says it might be. If the file is small, use this approach (and re-split into lines later). If it's big, use Tudor's. – Kevin Anderson Apr 03 '12 at 20:25
  • It shouldn't be massive. 50 accounts, couple of transactions per day. – MadBoy Apr 03 '12 at 20:26
  • Only problem is Split takes char not a string – MadBoy Apr 03 '12 at 20:32
  • Sorry I couldn't remember the exact overload off the top of my head. Fixed and source to the overload I'm, using. http://msdn.microsoft.com/en-us/library/tabh47cf.aspx – ClassicThunder Apr 03 '12 at 20:35
  • 1
    @MadBoy: You can try to use the overload that takes an array of strings: `string[] blocks = (file.ReadAllText(file)).split(new string[] {"\n\n\n"}, StringSplitOptions.None);` – Tudor Apr 03 '12 at 20:37
0

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

StreamReader _sr = new StreamReader("YourFile");
_sr.ReadLine();
Jmyster
  • 965
  • 2
  • 9
  • 27