Class FixedWidthFieldAttribute

Namespace
Wolfgang.Etl.FixedWidth.Attributes
Assembly
Wolfgang.Etl.FixedWidth.dll

Marks a property as a fixed-width field and specifies its column index and width. Apply this attribute to any property on a POCO that represents a single record in a fixed-width file.

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class FixedWidthFieldAttribute : Attribute
Inheritance
FixedWidthFieldAttribute
Inherited Members

Examples

public class CustomerRecord
{
    [FixedWidthField(0, 10)]
    public string FirstName { get; set; }

    [FixedWidthField(1, 10)]
    public string LastName { get; set; }

    [FixedWidthField(2, 5, Alignment = FieldAlignment.Right, Pad = '0')]
    public int ZipCode { get; set; }
}

// With a skipped column between fields:
public class EmployeeRecord
{
    [FixedWidthField(0, 10)]
    public string FirstName { get; set; }

    [FixedWidthSkip(1, 8, Message = "DOB")]
    [FixedWidthField(2, 5)]
    public string EmployeeNumber { get; set; }
}

Remarks

The start position of each field is calculated automatically by summing the Length values of all preceding columns in index order, including any columns declared with FixedWidthSkipAttribute.

Index is required and must be unique across all FixedWidthFieldAttribute and FixedWidthSkipAttribute instances on the type. Duplicate index values are detected at runtime by Wolfgang.Etl.FixedWidth.Parsing.FieldMap.

When writing (loading), string values longer than Length throw a FieldOverflowException. Values shorter than Length are padded according to Alignment and Pad.

Constructors

FixedWidthFieldAttribute(int, int)

Initializes a new instance of FixedWidthFieldAttribute.

public FixedWidthFieldAttribute(int index, int length)

Parameters

index int

The zero-based column index. Must be unique across all FixedWidthFieldAttribute and FixedWidthSkipAttribute instances on the type.

length int

The number of characters this field occupies in the fixed-width record. Must be greater than zero.

Properties

Alignment

The alignment used when padding this field during a write (load) operation. Defaults to Left (left-aligned, padded on the right).

public FieldAlignment Alignment { get; set; }

Property Value

FieldAlignment

Format

Optional format string applied when converting a non-string property to its string representation during a write (load) operation, or when parsing a raw string value back to the property type during a read (extract) operation. For example, "yyyyMMdd" for a DateTime field or "D5" for a zero-padded integer.

public string? Format { get; set; }

Property Value

string

Header

The column header label written when WriteHeader is true. If not set, the property name is used as the header label.

public string? Header { get; set; }

Property Value

string

Index

The zero-based column index used to order this field within the record.

public int Index { get; }

Property Value

int

Length

The number of characters this field occupies within the fixed-width record.

public int Length { get; }

Property Value

int

Pad

The character used to pad this field to its full Length during a write (load) operation. Defaults to ' ' (space).

public char Pad { get; set; }

Property Value

char

TrimValue

When true, leading and trailing whitespace is trimmed from the extracted value before it is assigned to the property. Defaults to true.

public bool TrimValue { get; set; }

Property Value

bool