Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reading Data from a multi sequenced File

Reading Data from a multi sequenced File

Scheduled Pinned Locked Moved C#
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MumbleB
    wrote on last edited by
    #1

    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
    
    OriginalGriffO P J 3 Replies Last reply
    0
    • M MumbleB

      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
      
      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      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.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      M 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        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.

        M Offline
        M Offline
        MumbleB
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • M MumbleB

          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
          
          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • M MumbleB

            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
            
            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups