16

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[];
  }
Marcos Meli
  • 3,338
  • 21
  • 29
Andrus
  • 22,189
  • 50
  • 171
  • 330

3 Answers3

42

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
Marcos Meli
  • 3,338
  • 21
  • 29
  • Thank you. Actually files can be in two different formats: fixed width as described in question and in record format where each field is in separate line. Fields are also different in boths formats so two different read modes should used. Fixed width format contains always "VV " in start of every line. How to examine stream before reading and apply different reading mode depending on format ? – Andrus Jun 13 '12 at 06:35
  • 1
    engine.Options.IgnoreFirstLines = 1 – zerohero Apr 19 '16 at 13:08
3

You could use the BeforeReadRecord event to check the format of the line. Set the SkipThisRecord property in the event data if it's the first record. Determining if it's the last record is something of a problem, but you could just check the length or format instead. Of course, that'll prevent you from catching errors caused by malformed records.

Another possibility is to load the entire file into memory using File.ReadAllLines. Remove the first and last items from the resulting array, turn it back into a string, and then call ReadString. Or you could write the strings to a MemoryStream and call ReadStream.

Jim Mischel
  • 122,159
  • 16
  • 161
  • 305
0

the best is to decorate your class with the [IgnoreFirst] attribute.

If for some reason you can't add the attribute you can do something like this

var engine = new FileHelperEngine<T>();
engine.BeforeReadRecord += (e, args) =>
{
    if (args.LineNumber == 1)
        args.SkipThisRecord = true;
};
Thiago Dantas
  • 664
  • 6
  • 16