I have a file that I stream into a list object to save to a temp in my database, because I have to use the data in the temp table to join to other tables for a final result, then export this final result to a .csv file.
Everything works except for the stream read of the original file.
The file is comma-delimited and structured very specifically, and the structure never changes.
The problem I have is this:
The "AccountHolder" field has a comma amongst the characters that make up the string, so my FileStream sees this as a delimiter. How do I replace the comma in the AccountHolder string without breaking the comma-delimiter the FileStream has to adhere to?
List<object[]> fileContent = new List<object[]>();
using (FileStream reader = File.OpenRead(ofd.FileName))
using (TextFieldParser parser = new TextFieldParser(reader))
{
parser.TrimWhiteSpace = true;
parser.Delimiters = new[] { "," };
parser.HasFieldsEnclosedInQuotes = true;
while (!parser.EndOfData)
{
object[] line = parser.ReadFields();
fileContent.Add(line);
lstRegNo.Add(line[0].ToString().Trim());
lstAccHolder.Add(line[1].ToString().Trim().Replace(',', ' '));
lstAmount.Add(line[2].ToString().Trim().Replace(',', ' '));
lstAccNo.Add(line[3].ToString().Trim());
lstBranch.Add(line[4].ToString().Trim());
lstDate.Add(line[5].ToString().Trim());
lstCode.Add(line[6].ToString().Trim());
lstOrphenColumn.Add(line[7].ToString().Trim());
}
Here's a sample of the file I'm streaming in:
000001,A WHATEVER,00000000001,0000000000001,000001,160510,17,0
000002,B WHATEVER,00000000002,0000000000002,000002,160510,17,0
000003,C, WHATEVER,00000000003,0000000000003,000003,160510,17,0
000004,D WHATEVER,00000000004,0000000000004,000004,160510,17,0
000005,E WHATEVER,00000000005,0000000000005,000005,160510,17,0
As you can see, on line 3, there's a comma in the AccountHolder's name. I need the value of this to be "C WHATEVER", not "C, WHATEVER" I want to eliminate that comma but still be able to stream the file into my List object splitting the fields in the file by commas.
Please note that the file's data will be different everytime I receive it, so simply looking for a static value won't cut it.
How do I do this?