Question

var columnNames = new string[] {"A","B","C"};  
var cb = new DelimitedClassBuilder("GeneratedClassName", ",") { IgnoreFirstLines = 0, IgnoreEmptyLines = true, Delimiter = "," };


foreach (var header in columnNames)
{                
    cb.AddField(header, typeof(string));

    cb.LastField.FieldQuoted = true;
    cb.LastField.QuoteChar = '"';
    cb.LastField.QuoteMode = QuoteMode.OptionalForRead;
    cb.LastField.FieldOptional = true;
}

var engine = new FileHelperEngine(cb.CreateRecordClass());
var dataTable = engine.ReadStringAsDT("123 abc Abhi");

In the above code snippet, Expected behaviour is that a datatable would be created. This data table should contain 3 columns A,B,C and one record. The record should have value for column A as "123 abc Abhi". What FileHelpersEngine is returning is data table with 3 columns A,B,C and one record (So far as expected). The record has value for column A as "123 abc Abh" (Note last character 'i' is trimmed). Why is the last character trimmed? Last character is not trimmed always. It is trimmed only for certain input strings. For e.g. If input string is "TestInput," it works fine. We are using FileHelpers version 1. Any reason why last character is trimmed?

Was it helpful?

Solution

It's a bug related to optional fields in older versions of the FileHelpers library.

If I remember correctly, one workaround is to add another (ignored) optional field.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top