FileHelpers - Handle Missing Values With FieldNullValue

  • Contact / Feedback
  • Introduction
  • Download
  • Quick Start
  • Must Read
  • Examples
    • QuickStart
      • Read Delimited File
      • Write Delimited File
      • Read Fixed File
      • Write Fixed File
      • Read or Write Record by Record
      • Autoproperties
    • Missing Values
      • Handle Missing Values with Nullable
      • Handle Missing Values With FieldNullValue
    • Attributes
      • FieldTrim
      • FieldOrder
      • FixedLengthRecord FixedMode.AllowLessChars
    • Converters
      • Enum Converter
      • Custom Converter
    • Events And Notification
      • INotifyRead Interface
      • INotifyWrite Interface
      • Before/After Read Event Handling
      • Before/After Write Event Handling
    • ErrorHandling
      • ErrorMode.ThrowException
      • ErrorMode.IgnoreAndContinue
      • ErrorMode SaveAndContinue
    • Advanced
      • Dynamic Engine Options
      • Multiple Delimiters
      • Multi Record Processing
      • Smart Format Detector
    • Sorting
      • Sort Big File with Record Class
      • Sort Big File without Record Class 1
      • Sort Big File without Record Class 2
    • MasterDetail
      • Master Detail Custom Selector
      • Master Detail Common Selector
  • Credits
  • Invite us a beer
  • Contribute
  • Source Code
  • Api Docs (Sandcastle)
  1. Examples
  2. Missing Values
  3. Handle Missing Values With FieldNullValue
Previous: Handle Missing Values with Nullable
Next: FieldTrim
Click button to edit
Missing Values: Handle Missing Values With FieldNullValue
How to read a file with some missing values and use the FieldNullValue attribute

If your files contain a field that can be empty

Input.txt
10248|VINET|04071996|32.38
10249|TOMSP||11.61
10250|HANAR|08071996|65.83
10251|VICTE|03041991|41.34
RecordClass.cs
[DelimitedRecord("|")]
public class Orders
{
    public int OrderID;

    public string CustomerID;

    [FieldConverter(ConverterKind.Date, "ddMMyyyy")]
    [FieldNullValue(typeof (DateTime), "1900-01-01")]
    public DateTime OrderDate;

    public decimal Freight;
}
Example.cs
var engine = new FileHelperEngine<Orders>();
var records = engine.ReadFile("Input.txt");

foreach (var record in records) {
    Console.WriteLine(record.CustomerID);
    if (record.OrderDate != new DateTime(1900, 01, 01))
        Console.WriteLine(record.OrderDate.ToString("dd/MM/yyyy"));
    else
        Console.WriteLine("No Date");
    Console.WriteLine(record.Freight);
}
Console
VINET
04/07/1996
32,38
TOMSP
No Date
11,61
HANAR
08/07/1996
65,83
VICTE
03/04/1991
41,34
Copyright © 2020 Devoo - Marcos Meli All rights reserved. Template Design by GeeksLabs