Skip to content

2. Multi Line File Format

Ninja edited this page Sep 5, 2019 · 1 revision

Case 2: Let us see how we can parse a delimiter separted file with header and footer rows.

Note: This file will have rows with line head to determine each row type. By default, the line heads are 'H' for header, 'D' for data and 'F' for footer respectively. all these line heads are configurable via the config.

|H|Department|Jun 23 2016  7:01PM|
|D|Mr|Jack Marias|Male|London|
|D|Dr|Bony Stringer|Male|New Jersey|
|D|Mrs|Mary Ward|Female||
|D|Mr|Robert Webb|||
|F|4 Records|

Configuration

The configuration is the same as before. We can override the default line heads by specifying the required line head attribute in the parser settings.

{
    "configSettings":{
        "parserSettings":{ "delimiter": { "value":"|"} ,
                           "lineHeaders": { 
                                "header":"H", 
                                "footer":"F", 
                                "data":"D" 
                            }
        },
        "providerSettings":{
                        "folderPath":"C:work",
                        "fileNameFormat":"File*.txt",
                        "archiveUponRead":"true",
                        "archiveFolder":"Archived"
        }		
    }	  
}

Code

Like before we need a line class to map to each type of the row in the file ie one for the header, footer and data line respectively.

We continue by creating two extra classes HeaderLine and FooterLine as follows.

 public class Header : FileLine
    {
        [Column(0)]
        public string Name { get; set; }
        [Column(1)]
        public DateTime Date { get; set; }
    } 

 public class Footer : FileLine
    {
        [Column(0)]
        public string FileRemarks { get; set; }
    } 

To parse the file you call the GetFiles() Method as follows -

var files = new Engine(configSettings).GetFiles<Header, Employee, Footer>();

The engine will parse the files and return a collection of File<Header, Employee, Footer> objects ie. one for each file parsed with strongly typed header, footer and data line arrays.