FileHelpers: How to skip first and last line reading fixed width text

Andrus picture Andrus · Jun 12, 2012 · Viewed 13k times · Source

Code below is used to read fixed width uploaded file content text file using FileHelpers in ASP .NET MVC2

First and last line lengths are smaller and ReadStream causes exception due to this. All other lines have proper fixed width. How to skipt first and last lines or other way to read data without exception ?

    [FixedLengthRecord()]
    class Bank
    {
        [FieldFixedLength(4)]
        public string AINETUNNUS;
        [FieldFixedLength(16)]
        public string TEKST1;
        [FieldFixedLength(3)]
        public string opliik;
        [FieldFixedLength(2)]
        public string hinnalis;
    };

    [AcceptVerbs(HttpVerbs.Post)]
    [Authorize]
    public ActionResult LoadStatement(HttpPostedFileBase uploadFile)
    {

        FileHelperEngine engine = new FileHelperEngine(typeof(Bank));
        var res = engine.ReadStream(new StreamReader(uploadFile.InputStream,
             Encoding.GetEncoding(1257))) as Bank[];
  }

Answer

Marcos Meli picture Marcos Meli · Jun 12, 2012

You can use these attributes

IgnoreFirst: Indicates the numbers of lines to be ignored at the begining of a file or stream when the engine read it.

[IgnoreFirst(1)] 
public class OrdersVerticalBar 
{ ...

IgnoreLast: Indicates the numbers of lines to be ignored at the end of a file or stream when the engine read it.

[IgnoreLast(1)] 
public class OrdersVerticalBar 
{ ...

You can access the values later with

engine.HeaderText
engine.FooterText