0

I am currently looking at splitting a CSV file that is read into an application by the comma, however, there is legitimate comma's held in double quotes that are getting split when i dont want them to be.

when using TextFieldParser this is reading the fields that I am wanting it to read, however its reading all the fields and then i am struggling to get them out on the correct lines.

 public string ParseCSVForFields(string dataFileName)
    {

        var sb = new StringBuilder();
        var line = new List<string>();
        using (TextFieldParser parser = new TextFieldParser(dataFileName))
        {
            parser.TextFieldType = FieldType.Delimited;
            parser.SetDelimiters(",");
            parser.HasFieldsEnclosedInQuotes = true;

            while (!parser.EndOfData)
            {
                //Processing row
                string currentRow = parser.ReadLine();
                string[] fields = parser.ReadFields();
                foreach (var field in fields)
                {
                //    this is where i am stuck
                }



            }

        }

        return null;

    }

any and all help would be very much appreciated.

thanks

4

1 回答 1

1

You are calling both ReadLine and ReadFields. That seems suspicious. Remove the ReadLine part.

于 2014-11-05T12:01:57.930 回答