Prev: Help with multi-dimensional arrays
Next: Urgent! Global and Very Successful company needs C# Developer!
From: Arne Vajhøj on 5 May 2010 21:33 On 05-05-2010 12:11, tk wrote: > I have a text file that I need to import into a datatable. It is comma > delimited (and the fields are enclosed in quotes). I use the following code > to read the file. > > reader = new StreamReader(txtFilePath.Text.Trim()); > > string data = reader.ReadToEnd(); > > > string linedelimeter = "\r\n"; > > string delieter = ","; > > string[] rows = data.Split(linedelimeter.ToCharArray()); > > foreach (string r in rows) > > { > > string[] items = r.Split(delimeter.ToCharArray()); > > } > > > The problem is if one of the fields has a comma in it, the split function > seperates it into 2 fields, even thou the field is in quotes. Is there a > way to handle the comma in the field besides manually parsing each line? You need something more advanced. Manually parsing may be the simplest solution. It is not that difficult and you get it exactly as you need it. I do not think regex or the ODBC driver capable of reading CSV will make the code more readable. Arne
First
|
Prev
|
Pages: 1 2 Prev: Help with multi-dimensional arrays Next: Urgent! Global and Very Successful company needs C# Developer! |