Click here to Skip to main content
15,037,009 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
VB
:58A:/C/81000098099CL
CBNINGLA
:72:/CODTYPTR/012
/CLEARING/0003
/SGI/DBLNNGLA


am trying to read the swift message above that comes in a text file, line :58A: and line :72:, am having a little issue. My code only reads line
:58A: like this C/81000098099CL,
but I want it to read down the line before getting to line :72:, in short, the output should be like this for line
:58A: C/81000098099CL CBNINGLA.

Same also for line :72:, this is because the messages come formatted in this form. This is my code below.

C#
if (line.StartsWith(":58A:"))
{
    inflow202.BENEFICIARY_INSTITUTION = line.Substring(5)
}

if (line.StartsWith(":72:"))
{
    inflow202.RECEIVER_INFORMATION = line.Substring(5);
}


Please how can I modify my present code to get my desired output, to enable me save the multiple lines as a string into the database.

:58A: == C/81000098099CL CBNINGLA
:72: == /CODTYPTR/012/CLEARING/0003/SGI/DBLNNGLA
Posted
Comments
johannesnestler 29-Apr-14 8:13am
   
Substring(5)? you just read 1 line? I assume line is the current line in a "Loop" through all lines? If so, just use a next/previouse/... line variable, read all needed Information from the variouse lines and "build" them together to the desired output, I can't guess what problem you might have with that - seems pretty straight forward

You should define the
  • starting markers (e.g. :58A:, :72:)

and the
  • ending markers (are they the same patterns used as starting markers?)


and collect all the input between a 'starting' and an 'ending' marker. Finally you can should remove the newlines.

All you need is a very simple state machine[^].
   
You may iterate through all lines like this:
C#
string text="";
foreach(string line in File.ReadAllLines("TextFile.txt")){

    if (line.StartsWith(":58A:"))
    {
           text = line.substring(5);
    }else if (line.StartsWith(":72:"))
    {
           inflow202.RECEIVER_INFORMATION = text;
           text = line.substring(5);
    }else{
           text+=line;
    }
}
inflow202.RECEIVER_INFORMATION = text;
   
C#
using (StreamReader sr = new StreamReader(stream))
{
       String line;
       int pointer = 0;

       while ((line = sr.ReadLine()) != null)
       {

       if (pointer == 0)
       {
            inflow202 = new InflowTempData202();
       }

       var allLines = sr.ReadToEnd();
       var firstIndex = allLines.IndexOf(":20");
       var secondIndex = allLines.IndexOf(":21");
       var thirdIndex = allLines.IndexOf(":58A");
       var fourthIndex = allLines.IndexOf(":72");
       var fifthIndex = allLines.IndexOf("-}");

       var firstValue = allLines.Substring(firstIndex +4, secondIndex - firstIndex -5);
       var secondValue = allLines.Substring(thirdIndex +5, fourthIndex - thirdIndex - 5);
       var thirdValue = allLines.Substring(fourthIndex +4, fifthIndex - fourthIndex - 5);


       var len1 = firstValue.Length;
       var len2 = secondValue.Length;
       var len3 = thirdValue.Length;


       inflow202.REFERENCE = firstValue;
       pointer = 1;
       inflow202.BENEFICIARY_INSTITUTION = secondValue;
       inflow202.RECEIVER_INFORMATION = thirdValue;
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900