简体   繁体   中英

Reading and writing file with various types with FileHelpers

How can I read / write the file below in the following classes with FileHelpers:

在此处输入图片说明

The following classes:

public class SegmentP
{
    public string RegistryType { get; set; }
    public string Country { get; set; }
}
public class Detail
{
    public int RegistryType { get; set; }
    public int CodeCustomer { get; set; }
    public double NominalValue { get; set; }
    public SegmentP SegmentP { get; set; }
}
public class BatchFooter
{
    public int RegistryType { get; set; }
    public int Counter { get; set; }
}
public class BatchHeader
{
    public int RegistryType { get; set; }
    public int CodeService { get; set; }
}
public class Batch
{
    public BatchHeader BatchHeader { get; set; }
    public List<Detail> Details { get; set; }
    public BatchFooter BatchFooter { get; set; }
}
public class FileFooter
{
    public int RegistryType { get; set; }
    public int Counter { get; set; }
    public int Total { get; set; }
}
public class FileHeader
{
    public int RegistryType { get; set; }
    public int CompanyCode { get; set; }
    public DateTime GenerationDate { get; set; }
}
public class FileExample
{
    public FileHeader FileHeader { get; set; }
    public List<Batch> Batches { get; set; } 
    public FileFooter FileFooter { get; set; }
}

I would like to load the entire file in the FileExample entity, possible?

var fileExample = new FileExample
{
    FileHeader = new FileHeader
    {
        RegistryType = 1,
        CompanyCode = 1,
        GenerationDate = new DateTime(2016, 6, 1)
    },
    Batches = new List<Batch>
    {
        new Batch
        {
            BatchHeader = new BatchHeader
            {
                RegistryType = 2,
                CodeService = 1
            },
            Details = new List<Detail>
            {
                new Detail
                {
                    RegistryType = 3,
                    CodeCustomer = 1,
                    NominalValue = 10,
                    SegmentP = new SegmentP
                    {
                        RegistryType = "P",
                        Country = "Brazil"
                    }
                }
            },
            BatchFooter = new BatchFooter
            {
                RegistryType = 4,
                Counter = 1
            }
        }
    },
    FileFooter = new FileFooter
    {
        RegistryType = 5,
        Counter = 1,
        Total = 1
    }
};

I'm new with FileHelpers and need a direction, as in the example I noticed that there is the option of Master/Detail, but in my example the file has more types Master/Detail. Thank you for any help

Out of the box, using FileHelpers for a format with a complex hierarchy is difficult.

FileHelpers provides two methods of handling multiple record types: the master/detail engine and the multi-record engine .

Unfortunately it is likely that you need both for your format. It would be hard to combine them without some further coding.

To be clear

  • the MasterDetailEngine caters for header/footer situation, but it currently supports only one detail type and only one level of nesting.
  • the MultiRecordEngine allows multiple record types. However, it treats each row as an unrelated record and the hierarchy (that is, which detail record belongs to which master record) would be hard to determine.

I'm not sure FileHelpers is the right tool here, but if you were intent on getting it to work it might be worth investigating the following:

  1. Parse the file once in order to split the batches. You might even be able to use FileHelpers to do this. Then you would have a bunch of records of Batch[] with a string property BatchContents .

  2. Then enumerate Batch[] and use the master/detail engine to process the BatchContents .

  3. For export - do the opposite. Export each batch as a master detail BatchContents and then concatentate them.

Of course there's still more work if your headers and footers contain counts, control totals, checksums, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM