Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a CSV file converter in C#

Status
Not open for further replies.

weberm

Programmer
Dec 23, 2002
240
US
I have an accounting application that uses three data files. These files contain a file header and file footer with data records grouped by sub-headers and sub-footers. The first is the Invoice File which comes from our business and contains invoices to be paid. The second is the Payment File which comes from the bank and contains payment transactions. The application processes these two files and generates a Paid File which contains invoices which have been paid. I want to create a converter for development and unit testing but am new to C# and OOP so I'm feeling a bit overwhelmed. Are there any examples like this out there? I am thinking I should create an abstract class for the files and then make inherited classes for each of the three file types, each with their own parsing and validating methods and a converter with an overloaded convert method that depends on the file types being passed to it. The converter module seems simple enough but I am struggling with implementing the file data and the parsing and validating methods. I am considering some sort of hierarchal tree structure but am I making things far too complicated?
 
I've had to do something similar with regard to consolidating price catalogs from a number of different vendors who all have their own data format (all usually CSV based though).

Your basic model seems sound, though we used an interface rather than an abstract class. The vendor classes each handled their own file downloading/processing according the the differing specifications each vendor gave us. The processing handled converting the disparate specifications to a unified specification we could actually use. We also used reflection to get all the classes that implemented this vendor interface so it could automatically run on each of the vendors.

Based on the description so far, I'm not seeing where a hierarchical tree structure would be helpful - but that all depends on your data models.
 
craigber said:
Thanks, but those are for files with one type of record. Mine look like:
Code:
File Header
Batch Header
Data records
Batch Footer
Batch Header
Data records
Batch Footer
...
File Footer

I have seen an implementation where there was a File Header class containing a List of Batch Headers and a Batch Header class containing a list of data records, but that doesn't sound very OOP and sounds like a nightmare to maintain.
 
Actually, that is very OOP - I have done this for a number of projects doing this type of thing. However, I would set up the classes to not just read the headers - they would include the footer information as well. The constructors for each class would then do the following:

File:
- Takes the open file as a parameter
- Reads the first (header) record, setting properties accordingly.
- Reads the next record.
- If the record is a batch header, creates a Batch object and adds it to the list of batches.
- If the record is a file footer, add/update properties as necessary and pass control back to the calling program which will close the file and continue with any other required processing.

Batch:
- Takes the string with the current record and the open input file as parameters.
- Sets the properties based on the data in the current record string.
- Reads the next record.
- If the record is a data record, create a Data object and add it to list of data for this batch. Repeat these two steps until reaching the batch footer.
- If the record is a batch footer, update the footer properties for the batch and return to the File constructor.

Data:
- Takes the string with the current record and sets the properties accordingly.
- Passes control back to the Batch constructor.

-Dell

DecisionFirst Technologies - Seven-time SAP BusinessObjects Solution Partner of the Year
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top