Reading Data from a multi sequenced File
-
Hi Guys. I have a file with two sequences, Sequence A and a Sequence B. Sequence A only appears once in the file and Sequence B can appear once or multiple times in the file. See Sample data below:
:21:REFERENCE1 :23E:OTHR/DMST :32B:ZAR5005,00 :57A:CODEXXXX :59:/12345600002345678 TEST NAME2 ADDRESS1 ADDRESS2 ADDRESS3 :21:REFERENCE2 :23E:OTHR/DMST :32B:ZAR7005,00 :57A:CODEAAAA :59:/12345657002345678 TEST NAME3 ADDRESS1 ADDRESS2 ADDRESS3 :71A:OUR -}
The portion in Bold is Sequence A and the remainder is sequence B data. As you can see by the "TAGS" the fields starting with ':', that there are multiple Sequence B data. I need a way to pick up the data for the multiple Sequence B records. Sequence B start with :21: and ends with :59: followed by the address details. I tried a straight forward StreamReader and then populating the data but my Steamreader always goes from top to bottom in my while loop. Sample below.string inFile; inFile = textBox1.Text; StreamReader sr = new StreamReader(inFile); string holdline; int lineCount = 0; int clientname = 0; int creditname = 0; while (!sr.EndOfStream) { holdline = sr.ReadLine(); if (holdline.Length > 4) { string tag = holdline.Substring(0, 4); string tag2 = holdline.Substring(0, 5); if (tag == "{1:F") { lblBIC1.Text = holdline.Substring(6, 8).Trim(); lblClientBIC.Text = holdline.Substring(46, 8).Trim(); lblMsgType.Text = "MT" + holdline.Substring(33, 3).Trim(); lblDate.Text = holdline.Substring(36, 10).Trim(); lblSWIFTRef.Text = holdline.Substring(58, 10).Trim(); } if (tag == ":20:") { int results = holdline.Length - 4; lblA20.Text = holdline.Substring(4, results).Trim(); } else if (tag2 == ":21R:") { int r1 = holdline.Length - 5; lblA20.Tex
-
Hi Guys. I have a file with two sequences, Sequence A and a Sequence B. Sequence A only appears once in the file and Sequence B can appear once or multiple times in the file. See Sample data below:
:21:REFERENCE1 :23E:OTHR/DMST :32B:ZAR5005,00 :57A:CODEXXXX :59:/12345600002345678 TEST NAME2 ADDRESS1 ADDRESS2 ADDRESS3 :21:REFERENCE2 :23E:OTHR/DMST :32B:ZAR7005,00 :57A:CODEAAAA :59:/12345657002345678 TEST NAME3 ADDRESS1 ADDRESS2 ADDRESS3 :71A:OUR -}
The portion in Bold is Sequence A and the remainder is sequence B data. As you can see by the "TAGS" the fields starting with ':', that there are multiple Sequence B data. I need a way to pick up the data for the multiple Sequence B records. Sequence B start with :21: and ends with :59: followed by the address details. I tried a straight forward StreamReader and then populating the data but my Steamreader always goes from top to bottom in my while loop. Sample below.string inFile; inFile = textBox1.Text; StreamReader sr = new StreamReader(inFile); string holdline; int lineCount = 0; int clientname = 0; int creditname = 0; while (!sr.EndOfStream) { holdline = sr.ReadLine(); if (holdline.Length > 4) { string tag = holdline.Substring(0, 4); string tag2 = holdline.Substring(0, 5); if (tag == "{1:F") { lblBIC1.Text = holdline.Substring(6, 8).Trim(); lblClientBIC.Text = holdline.Substring(46, 8).Trim(); lblMsgType.Text = "MT" + holdline.Substring(33, 3).Trim(); lblDate.Text = holdline.Substring(36, 10).Trim(); lblSWIFTRef.Text = holdline.Substring(58, 10).Trim(); } if (tag == ":20:") { int results = holdline.Length - 4; lblA20.Text = holdline.Substring(4, results).Trim(); } else if (tag2 == ":21R:") { int r1 = holdline.Length - 5; lblA20.Tex
Try adding "+" characters:
lblA20.Text += holdline.Substring(4, results).Trim(); ... lblA20.Text += holdline.Substring(4, r1).Trim(); ... lblA50H.Text += holdline.Substring(6, r3).Trim();
Would be better if you used StringBuilder, but hey ho!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
Try adding "+" characters:
lblA20.Text += holdline.Substring(4, results).Trim(); ... lblA20.Text += holdline.Substring(4, r1).Trim(); ... lblA50H.Text += holdline.Substring(6, r3).Trim();
Would be better if you used StringBuilder, but hey ho!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Hi Griff. Not sure if that will work for what I have planned. What I am currently doing is writing the data to labels. Sequence B can be made up of multiple records/lines, each containing a set of TAGS. Tags are anything that start and end with "
:
". From the sample I supplied, one sequence B records starts with TAG :21: and ends with TAG :70:Excellence is doing ordinary things extraordinarily well.
-
Hi Guys. I have a file with two sequences, Sequence A and a Sequence B. Sequence A only appears once in the file and Sequence B can appear once or multiple times in the file. See Sample data below:
:21:REFERENCE1 :23E:OTHR/DMST :32B:ZAR5005,00 :57A:CODEXXXX :59:/12345600002345678 TEST NAME2 ADDRESS1 ADDRESS2 ADDRESS3 :21:REFERENCE2 :23E:OTHR/DMST :32B:ZAR7005,00 :57A:CODEAAAA :59:/12345657002345678 TEST NAME3 ADDRESS1 ADDRESS2 ADDRESS3 :71A:OUR -}
The portion in Bold is Sequence A and the remainder is sequence B data. As you can see by the "TAGS" the fields starting with ':', that there are multiple Sequence B data. I need a way to pick up the data for the multiple Sequence B records. Sequence B start with :21: and ends with :59: followed by the address details. I tried a straight forward StreamReader and then populating the data but my Steamreader always goes from top to bottom in my while loop. Sample below.string inFile; inFile = textBox1.Text; StreamReader sr = new StreamReader(inFile); string holdline; int lineCount = 0; int clientname = 0; int creditname = 0; while (!sr.EndOfStream) { holdline = sr.ReadLine(); if (holdline.Length > 4) { string tag = holdline.Substring(0, 4); string tag2 = holdline.Substring(0, 5); if (tag == "{1:F") { lblBIC1.Text = holdline.Substring(6, 8).Trim(); lblClientBIC.Text = holdline.Substring(46, 8).Trim(); lblMsgType.Text = "MT" + holdline.Substring(33, 3).Trim(); lblDate.Text = holdline.Substring(36, 10).Trim(); lblSWIFTRef.Text = holdline.Substring(58, 10).Trim(); } if (tag == ":20:") { int results = holdline.Length - 4; lblA20.Text = holdline.Substring(4, results).Trim(); } else if (tag2 == ":21R:") { int r1 = holdline.Length - 5; lblA20.Tex
If it's not too big, you could read the whole thing in and use a RegularExpression to parse it. I would likely read it one character at a time rather than as lines. You can then write a little Finite State Machine. You are also trying to do two things in one method here; I recommend splitting the method in two -- one to read/parse, one to display. The reader/parse could return a List of Tuples, each Tuple wouyld contain the type/number of the field and the data.
-
Hi Guys. I have a file with two sequences, Sequence A and a Sequence B. Sequence A only appears once in the file and Sequence B can appear once or multiple times in the file. See Sample data below:
:21:REFERENCE1 :23E:OTHR/DMST :32B:ZAR5005,00 :57A:CODEXXXX :59:/12345600002345678 TEST NAME2 ADDRESS1 ADDRESS2 ADDRESS3 :21:REFERENCE2 :23E:OTHR/DMST :32B:ZAR7005,00 :57A:CODEAAAA :59:/12345657002345678 TEST NAME3 ADDRESS1 ADDRESS2 ADDRESS3 :71A:OUR -}
The portion in Bold is Sequence A and the remainder is sequence B data. As you can see by the "TAGS" the fields starting with ':', that there are multiple Sequence B data. I need a way to pick up the data for the multiple Sequence B records. Sequence B start with :21: and ends with :59: followed by the address details. I tried a straight forward StreamReader and then populating the data but my Steamreader always goes from top to bottom in my while loop. Sample below.string inFile; inFile = textBox1.Text; StreamReader sr = new StreamReader(inFile); string holdline; int lineCount = 0; int clientname = 0; int creditname = 0; while (!sr.EndOfStream) { holdline = sr.ReadLine(); if (holdline.Length > 4) { string tag = holdline.Substring(0, 4); string tag2 = holdline.Substring(0, 5); if (tag == "{1:F") { lblBIC1.Text = holdline.Substring(6, 8).Trim(); lblClientBIC.Text = holdline.Substring(46, 8).Trim(); lblMsgType.Text = "MT" + holdline.Substring(33, 3).Trim(); lblDate.Text = holdline.Substring(36, 10).Trim(); lblSWIFTRef.Text = holdline.Substring(58, 10).Trim(); } if (tag == ":20:") { int results = holdline.Length - 4; lblA20.Text = holdline.Substring(4, results).Trim(); } else if (tag2 == ":21R:") { int r1 = holdline.Length - 5; lblA20.Tex
Kwagga wrote:
Any advice would be greatly apreciated.
Write a parser which reads the file and creates a data structure (presuming that the file is reasonably sized.) Your data struct is represented by the following pseudo code
class RecordA { // Fields from record ... } class RecordB { // Fields from record ... } class Data { RecordA RecordA List RecordBList }
After you have populated Data above then you pass it to something else.