简体   繁体   中英

FileHelpers - extra column in file not raising an error when FieldQuoted attribute used

Using FileHelpers.dll version 3.0.1.0, with .net 4.0.

Code to reproduce issue:

create a file Accounts.txt like this:

"AccountName","ExtraColumn"
"MR GREEN ","abc"
"MR SMITH ","def"

c# Account class :

[IgnoreFirst(1)]
[DelimitedRecord(",")]
[IgnoreEmptyLines()]
public class Account
{
    [FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.NotAllow)]
    public string AccountName;

}

To read the file:

        string fname = @"C:\test\Accounts.txt";

        FileHelperEngine engine = new FileHelperEngine(typeof(Account));

        Account[] importNodes =  (Account[])engine.ReadFile(fname);  // XX

Now, I would have expected an exception to be raised at line XX, because there are more columns in the file (2), than columns in the "Account" class (1). However, there is no exception raised, and the extra column seems to get silently ignored.

If you change the Account class to remove the "FieldQuoted" attribute, then an exception is indeed raised, like:

[FileHelpers.BadUsageException] {"Line: 2 Column: 0. Delimiter ',' found after the last field 'AccountName' (the file is wrong or you need to add a field to the record class)"} FileHelpers.BadUsageException

Can anyone provide some insight ? Should the code with the FieldQuoted attribute indeed raise an exception ? Am I doing something wrong ?

EDIT : are there any workarounds, so that an error will be raised when there are more columns in the input file than expected ?

[IgnoreFirst(1)] ignores first line of a file, not the first column.

See description here: IgnoreFirstAttribute

Removing FieldQuoted causes error because you have quoted fields that can't be handled correctly without this attribute.

FileHelpers library just ignores the fields from file that don't have corresponding fields in your class.

To ignore first field in each row, you can just add a dummy field to your class definition like this:

[IgnoreFirst(1)]
[DelimitedRecord(",")]
[IgnoreEmptyLines()]
public class Account
{
   [FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.NotAllow)]
   public string AccountName;

   [FieldValueDiscarded]
   [FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.NotAllow)]
   public string Dummy;

}

As a workaround I can suggest to add an ordinary string dummy field at the end. You can then check if this field is filled in FileHelperEngine.AfterReadRecord and throw an exception like this:

FileHelperEngine engine = new FileHelperEngine(typeof(Account));
engine.AfterReadRecord += engine_AfterReadRecord;
try
{
    Account[] importNodes = (Account[])engine.ReadFile(fname);  // XX
}
catch (Exception e)
{

}


static void engine_AfterReadRecord(EngineBase engine, FileHelpers.Events.AfterReadEventArgs<object> e)
{
    if (!string.IsNullOrEmpty(((Account) e.Record).Dummy))
    {
        throw new ApplicationException("Unexpected field");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM