FileHelpers: mixing fixed-length delimited records

Here I have to write a file whose records are split into pipes using FileHelpers and C #. Most of the fields are variable in length (so my records will be [DelimitedRecord ("|")]). But some fields should have a fixed length (they should have paddings, a specific format, etc.).

I have a googled bunch with no goal on how to do this.

Example:

[DelimitedRecord("|")]
public class Customer
{
    public int CustId; //variable length

    public string Name; //variable length

    public decimal Balance; //variable length

    [FieldConverter(ConverterKind.Date, "dd-MM-yyyy")]
    public DateTime AddedDate;

    public int Code; // this one must have 10 characters with "zero-fill", like
             // 153 must look like 0000000153

}

How to do it? Should I use the converter approach and write my own converter for it?

Thanks in advance.

+4
source share
3 answers

As @TYY mentioned, I wrote my own "multiuse" converter, like this:



    public StringNumberCharConverter(
        string Size, 
        string PaddingChar, 
        string PaddingType, 
        string RemoveSpecialChars)
    { 
        //implementation here 
    }

FileHelpers , .

"Size" "integer", PaddingChar "char", PaddingType (: Padding.LEFT Padding.RIGHT, , "left" , String.PadLeft() ..), "RemoveSpecialChars" (, , .)

Object-to-File, FieldToString ConverterBase.

0

, , - .

, FileHelper , 0s , .

public class PaddedIntConverter:ConverterBase
{
    private int _size;
    public PaddedIntConverter(int size)
    {
        _size = size;
    }

    public override object StringToField(string from)
    {
        return int.Parse(from);
    }

    public override string FieldToString(object from)
    {
        return from.ToString().PadLeft(_size,'0');
    }
}

:

[FixedLengthRecord(FixedMode.ExactLength)]
public class MyClass{
    [FieldFixedLength(7)]
    [FieldConverter(typeof(PaddedIntConverter), 7)]
    public int RecordCount;
}
+4

FileHelpers has the attribute [FieldFixedLength (xxx)], I believe that this should get what you are looking for ( http://filehelpers.sourceforge.net/attributes.html ).

+1
source

All Articles