question

PolachanPaily-6650 avatar image
0 Votes"
PolachanPaily-6650 asked JackJJun-MSFT commented

Read each string from a string array

Hi

I am trying to store each string into a variable from a string array. I have a string like



  string[]  fileContent = ftp.ReadFiles(cellValue); fileContent   array holding the values as given below
        
         "1","Truck","FN60HZU","'WC",,"26/03/2022","H2","KS","2","29580225","GY","KMAXD","12","12","12",,"17","5TST001","16:01:00","16:07:16","40:D3:AE:CB:16:EE"
         "2","Truck","YD17AMM","'WC",,"26/03/2022","H2","KS","1","31570225","BS","DURAVIS","11","11","11",,"17","5TST001","15:55:31","16:00:11","40:D3:AE:CB:16:EE"
         "2","Truck","YD17AMM","'WC",,"26/03/2022","H2","KS","1","31570225","BS","DURAVIS","11","11","11",,"17","5TST001","15:55:31","16:00:11","40:D3:AE:CB:16:EE"
     foreach (string s in fileContent)
     {
               string vehicleType = "Truck",
               string regno = ""FN60HZU"
              ..................................
             ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     }

How can I store each string into a variable ?
Regards






dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@PolachanPaily-6650, is any update? Please check if my answer works for you.

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

Consider using CsvHelper, in short you create a class/model to represent your data than read it. and if you do not want quotes use csv.Configuration.IgnoreQuotes = true;

 void Main()
 {
     using (var reader = new StreamReader("path\\to\\file.csv"))
     using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
     {
         var records = csv.GetRecords<Foo>();
     }
 }
    
 public class Foo
 {
     public int Id { get; set; }
     public string Name { get; set; }
 }



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PolachanPaily-6650 avatar image
0 Votes"
PolachanPaily-6650 answered

Hi karenpayneoregon

I am trying to parse the data without using csv helper. I want to get each string into a a variable from an array string

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered PolachanPaily-6650 commented

You can get all items as shown below, here I get one part of a line into an anonymous type VehicleType is a string.

 var results = (from line in fileContent where line.Length > 0
     let Items = line.Split(',') select new { VehicleType = Items[0] }).ToList();
    
 foreach (var item in results)
 {
     Console.WriteLine(item.VehicleType);
 }

As you can guess we need to do conversions if a property is not a string e.g.

 var results = (from line in fileContent where line.Length > 0
     let Items = line.Split(',') select new { VehicleType = Items[0], SomeOtherProperty = Convert.ToDateTime(Items[1]) }).ToList();


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

it will not work . Some data might come "'WC",,"26/03/2022", Please can you help me with the attached csv file
I have attached my sample csv file here with202012-capture.png


0 Votes 0 ·
capture.png (38.5 KiB)
JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered

@PolachanPaily-6650, Welcome to Microsoft Q&A, we could create a class for the csv file.

Then, we need to remove " " for every variable.

Here is a code example you could refer to.

  static void Main(string[] args)
         {
             string[] fileContent = File.ReadAllLines(@"C:\Users\username\Desktop\t.csv").Select(i=>i.Replace("\"", "")).ToArray();
             var result=(from line in fileContent
                        let Items = line.Split(',')
                        select new CsvContent 
                        {   
                            Col1 =Convert.ToInt32(Items[0]),
                            Col2 = Items[1],
                            Col3 = Items[2],
                            Col4 = Items[3],
                            Col5 = Items[4],
                            Col6 = DateTime.ParseExact(Items[5], "dd/MM/yyyy", CultureInfo.InvariantCulture),
                            Col7 = Items[6],
                            Col8 = Items[7],
                            Col9 =Convert.ToInt32( Items[8]),
                            Col10 = Convert.ToInt32(Items[9]),
                            Col11 = Items[10],
                            Col12= Items[11],
                            Col13= Convert.ToInt32(Items[12]),
                            Col14 = Convert.ToInt32(Items[13]),
                            Col15 = Convert.ToInt32(Items[14]),
                            Col16 = Items[15],
                            Col17= Convert.ToInt32(Items[16]),
                            Col18 = Items[17],
                            Col19= DateTime.ParseExact(Items[18], "HH:mm:ss", CultureInfo.InvariantCulture),
                            Col20 = DateTime.ParseExact(Items[19], "HH:mm:ss", CultureInfo.InvariantCulture),
                            Col21 = Items[20]
    
    
                        }).ToList();
    
    
         }
    
         public class CsvContent
         {
             public int Col1 { get; set; } 
    
             public string Col2 { get; set; }
    
             public string Col3 { get; set; }
             public string Col4 { get; set; }
             public string Col5 { get; set; }
             public DateTime Col6 { get; set; }
             public string Col7 { get; set; }
             public string Col8 { get; set; }
             public int Col9 { get; set; }
             public int Col10 { get; set; }
             public string Col11 { get; set; }
             public string Col12 { get; set; }
             public int Col13 { get; set; }
             public int Col14 { get; set; }
             public int Col15 { get; set; }
             public string Col16 { get; set; }
             public int Col17 { get; set; }
             public string Col18 { get; set; }
             public DateTime Col19 { get; set; }
    
             public DateTime Col20 { get; set; }
             public string Col21 { get; set; }
    
    
    
         }

Tested result:

203194-image.png



Best Regards,
Jack


image.png (45.4 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.