/// <summary> /// Try to parse a line of CSV data. Can only return false if an unterminated text qualifier is encountered. /// /// This function cannot recognize 'sep=' lines because it does not know whether it is parsing the first line /// in the overall CSV stream. /// </summary> /// <returns>False if there was an unterminated text qualifier in the <paramref name="line"/></returns> /// <param name="line">The line of text to parse</param> /// <param name="settings">The CSV settings to use for this parsing operation (Default: CSV)</param> /// <param name="row">The array of fields found in the line</param> public static bool TryParseLine(string line, out string[] row, CSVSettings settings = null) { row = null; var machine = new CSVStateMachine(settings); while (machine.State == CSVState.CanKeepGoing) { row = machine.ParseChunk(line, true); line = string.Empty; } return(machine.State == CSVState.Done); }
/// <summary> /// Parse a line from a CSV file and return an array of fields, or null if it fails /// </summary> /// <param name="line">One line of text from a CSV file</param> /// <param name="settings">The CSV settings to use for this parsing operation (Default: CSV)</param> /// <param name="throwOnFailure">If this value is true, throws an exception if parsing fails</param> /// <returns>An array containing all fields in the next row of data, or null if it could not be parsed.</returns> public static string[] ParseLine(string line, CSVSettings settings = null, bool?throwOnFailure = null) { string[] row = null; var machine = new CSVStateMachine(settings); while (machine.State == CSVState.CanKeepGoing) { row = machine.ParseChunk(line, true); line = string.Empty; } // Anything other than success throws an error here if (machine.State != CSVState.Done) { throw new Exception($"Malformed CSV structure: {machine.State}"); } return(row); }