Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Creating a CSV file converter in C#

Creating a CSV file converter in C#

Creating a CSV file converter in C#

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?

RE: Creating a CSV file converter in C#

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.

RE: Creating a CSV file converter in C#


Quote (craigber)

Thanks, but those are for files with one type of record. Mine look like:


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.

RE: Creating a CSV file converter in C#

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:

- 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.

- 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.

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


DecisionFirst Technologies - Seven-time SAP BusinessObjects Solution Partner of the Year

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close