4

I'm parsing a tab delimited file using FileHelpers.

The null values are being ignored after using the FieldNullValue attribute and I am ending up with the error log

can't be found after the field 'filed name' at line 4 (the record has less fields, the delimiter is wrong or the next field must be marked as optional).

Class definition of delimiter:

[DelimitedRecord("\t")]

Fields are all strings with the same attributes:

 [FieldTrim(TrimMode.Both)]
 [FieldNullValue("NULL")]
 [FieldQuoted('"', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
 public String initials;

Looking at the imported file in a hex editor i can see back to back tab chars (09 09) which would I assume be a null field.

file line in hex view

As you can see in the screen capture fields 5 & 9 are null. These get ignored by the filehelper parser. Does anyone know why?

R Davies
  • 105
  • 1
  • 11
  • What is the question? – Mert Gülsoy Aug 28 '14 at 12:48
  • why are the null values not being replaced by the FieldNullValue attribute instead of throwing errors about the number of fields – R Davies Aug 28 '14 at 12:55
  • do you mean changing the input format? if so then its not an option. I am considering the route of editing the file programmatically first to replace all double tabs to a blank string but didn't really wan tot go down that route. – R Davies Aug 28 '14 at 14:02
  • Does your model allow nullable fields for field5 & 9 ? – Mert Gülsoy Aug 28 '14 at 14:27

2 Answers2

3

I think you have two problems going on.

Firstly, FileHelpers is expecting one more tab. One easy fix is to mark the last field with the [FieldOptional] attribute.

Secondly, FieldNullValue("NULL") means: If the value of the field in the file is null, set it to the string "NULL". The value in your file is "", not null. If you need to convert empty values to something else, you can use a custom converter as follows:

public class MyEmptyFieldConverter : ConverterBase
{
    protected override bool CustomNullHandling
    {
        /// you need to tell the converter not 
        /// to handle empty values automatically
        get { return true; } 
    }

    public override object StringToField(string from)
    {
        if (String.IsNullOrWhiteSpace(from))
            return "NULL";
        return from;
    }
}

And then add the attribute to your field.

[FieldConverter(typeof(MyEmptyFieldConverter))]
public string field9;
shamp00
  • 11,106
  • 4
  • 38
  • 81
1

removing the attribute:

[FieldTrim(TrimMode.Both)]

Seems to have solved the problem.

R Davies
  • 105
  • 1
  • 11