Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..i am working on parsing a textfile.i am stuck at one place..i want to know how can i parse this text part of text file..i want to skip the headers(item,visa,etc..) and(No|Amt) and the headings on left side of table SALES(STD),SALES(ELC),etc.. i want to insert only the numeric values in database along with MerchantNo and OutletName
MERCHANT NO :  105838015                                                                                          
  OutletName    :  Big Bazzar         
                 
ITEM          VISA             MASTERCARD      DINERS             TOTAL                 
------------------------------------------------------------------------------------------------------------------------------------
                                                                                                                                    
            | NO|    AMT   |  NO|    AMT   |  NO|    AMT   |    NO|    AMOUNT         
------------------------------------------------------------------------------------------------------------------------------------
SALES(STD) |   0|      0.00|   0|      0.00|   0|      0.00|      0|         0.00      
SALES(ELC) |  10|  16466.78|   9|  16573.86|   0|      0.00|     19|     33040.64      
REFUND     |   1|   -371.20|   0|      0.00|   0|      0.00|      1|      -371.20      
DISC/STD   |   0|      0.00|   0|      0.00|   0|      0.00|      0|         0.00      
DISC/ELC   |  10|   -238.77|   9|   -240.34|   0|      0.00|     19|      -479.11      
DISC/REF   |   1|      6.12|   0|      0.00|   0|      0.00|      1|         6.12      
ACCRUED    |   0|      0.00|   0|      0.00|   0|      0.00|      0|         0.00     


This is the part of code which i have wriiten for the textfile for parsing other data in textfile..
C#
foreach (string item in str)
            {
                if (item.Trim().Contains("MERCHANT NO :"))
                {
                    string MNo1 = item.Substring(15, 23).Trim();
                    MNo = Convert.ToInt32(MNo1);
                }

                //condition for retrieving records from textfile
                if (item.Contains("XXXX"))
                { //splitting each and every line to an array based on space between the data.
                    char[] separators = new char[] { ' ' };
                    string[] rec = item.Trim().Split(separators, StringSplitOptions.RemoveEmptyEntries); //44

                    for (int i = 0; i < rec.Length; i++)
                    {
                        if (!String.IsNullOrEmpty(rec[i]))
                        {
                            string Date = rec[0].ToString();
                            AuthDate = Convert.ToDateTime(Date);
                            BNo = rec[1].ToString();
                            SNo = rec[2].ToString();
                            CardNo = rec[3].ToString();
                            TranType = rec[4].ToString();
                            RefNumber = rec[5].ToString();
                            AuthCode = rec[6].ToString();
                            string Amount = rec[7].ToString();
                            TranAmount = Convert.ToDecimal(Amount);

                            SqlConnection con = new SqlConnection("Data Source=COMPUTER-8EB749;Initial Catalog=TRMSDB;Integrated Security=true");
                            con.Open();
                            string columnnames = "MerchantNo,AuthDate,BTNo,SeqNo,CardNo,TranType,RefNo,AuthCode,TranAmt";
                            string InsertQuery = String.Format("insert into TblBank ({0}) values ('{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}')", columnnames, MNo, AuthDate, BNo, SNo, CardNo, TranType, RefNumber, AuthCode, TranAmount);
                            SqlCommand cmd = new SqlCommand(InsertQuery, con);
                            cmd.ExecuteNonQuery();
                        }
                        break;
                    }

                }

                //What logic can i apply here..
                 if(item.StartsWith)
                 {
                       char[] separators = new char[] {'|'};
                       string[] amount = item.Trim().Split(separators);

                       for (int j = 0; j < amount.Length; j++)
                       {
                         if (!String.IsNullOrEmpty(amount[j]))
                         { 
                         
                         }
                       }
                     } }

Do help..
Posted
Updated 21-Sep-11 23:13pm
v2

1 solution

C#
string[] parts = str.Split("\r\n");

string merchantNumber = "";
string outletName     = "";
int    pos            = -1;

foreach(string line in parts)
{
    if (line.Contains("MERCHANT NO"))
    {
        pos = line.IndexOf(":");
        if (pos >= 0)
        {
            merchantNumber = line.SubString(pos).Trim();
        }
    }
    if (line.Contains("OutletName"))
    {
        pos = line.IndexOf(":");
        if (pos >= 0)
        {
            outletName = line.SubString(pos).Trim();
        }
    }
    if (!string.IsNullOrEmpty(merchantName) && !string.IsNullOrEmpty(outletname))
    {
        break;
    }
}

for int (i = parts.Length - 1; i >= 0; i--)
{
    if (!parts[i].Contains("-----")
    {
        HandleData(parts[i]);
    }
    else
    {
        break;
    }
}

private void HandleData(string data)
{
    string[] parts = data.Trim.Split("|");
    for (int i=0; i<=parts.Length; i++)
    {
        switch (i)
        {
            case 0 : // name
            case 1 : // NO 
            case 2 : // AMT
            ... etc...
        }
    }
}
 
Share this answer
 
v2

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