FileHelpers - FieldTrim

  • 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. Attributes
  3. FieldTrim
Previous: Handle Missing Values With FieldNullValue
Next: FieldOrder
Click button to edit
Attributes: FieldTrim
How to use the [FieldTrim] attribute (useful for fixed length records):
Input.txt
01010 Alfreds Futterkiste          13122005
12399 Ana Trujillo Emparedados y   23012000
00011 Antonio Moreno Taquería      21042001
51677 Around the Horn              13051998
99999 Berglunds snabbköp           02111999
//-> You first declare a Record Mapping class:
RecordClass.cs
[FixedLengthRecord()]
public class Customer
{
    [FieldFixedLength(5)]
    public int CustId;

    [FieldFixedLength(30)]
    [FieldTrim(TrimMode.Both)]
    public string Name;

    [FieldFixedLength(8)]
    [FieldConverter(ConverterKind.Date, "ddMMyyyy")]
    public DateTime AddedDate;
}
//-> Then iterate over the records:
Example.cs
var engine = new FixedFileEngine<Customer>();
var result = engine.ReadFile("input.txt");

foreach (var detail in result)
    Console.WriteLine(" Client: {0},  Name: '{1}'", detail.CustId, detail.Name);
Console
Client: 1010,  Name: 'Alfreds Futterkiste'
Client: 12399,  Name: 'Ana Trujillo Emparedados y'
Client: 11,  Name: 'Antonio Moreno Taquería'
Client: 51677,  Name: 'Around the Horn'
Client: 99999,  Name: 'Berglunds snabbköp'
Copyright © 2020 Devoo - Marcos Meli All rights reserved. Template Design by GeeksLabs