How to add additional headers/columns to CSV file?
Using CSVHelper to manipulate a CSV file but when I try to add additional columns of information I get the error, I thought this would be really simple to add.
Error: Header with name 'Payee_Country'[0] was not found.
Header with name 'Payer_Address1'[0] was not found.
Header with name 'Payer_Address2'[0] was not found.
Header with name 'Payer_City'[0] was not found.
Header with name 'Payer_State' [0] was not found.
Header with name 'Payer_Zip'[0] was not found.
Header with name 'Payer_Country'[0] was not found.
9 Replies
CSVHelper doesnt by default allow missing headers
but if you create a config and set it up correctly, it allows it just fine
note how I set both
HeaderValidated
and MissingFieldFound
to null here
this allows me to parse a file that doesnt match my record class.
HOWEVER
this is a bad idea
If you know the CSV file doesnt contain these values and you want to add them, you should have two models
one without and one with the new fields
then simply read the values, then use .Select
or some other method to turn your objects from A into B, adding the data as needed, then save the list of BsCould you show me how to do that?
I'm struggling a bit here
Thank you, I'm not quite sure how to add this into my code but I'll give it a go!
step 1 is to just make a record type that has only the headers and values that are in your current csv file
then you make the second record type that has the ones you want in your output
then its as simple as making a method that converts from type 1 into type 2, passing that method to
.Select
(I used a lambda, but you can pass a real method too) and you're pretty much doneCould you help me put this into my code? I'm banging my head trying to do this, it's been a while since I was last working on C# and this code
This is very basic stuff, except maybe the
.Select
call but I can help you with that
have you made the two models and the mapping method?
@SCOLANATOR ?@Pobiega Sorry for the delayed response, had something come up. This was what i've put together so far but there's an issue with line 188