Using CsvWriter without a header record

Feb 5, 2010 at 4:39 PM

Is this possible?

My issue revolves around the fact that the file I'm reading and trying to modify has a format completely outside my control (comes from a banking institution).  They have decided to have a set of two columns repeat 3 times without changing the header name, something like:

RecordId         EmployeeID    col1    col2        col1    col2        col1    col2 


So, I can easily read this into a collection that allows me to iterate and view/display the contents by simply not reading the header record.  However, the DataRecords are read-only so I cannot modify the collection of DataRecords and then pass it to the WriteDataRecords() method of the CsvWriter, as I had hoped to do.  All the modification will be programmatic with the user basically choosing a .CSV file and clicking 'Go' so the UI really doesn't matter (and I could use WinForms, but I usually prefer WPF).

I guess my question is, is there any easy/quick way for me to perform the following functionality: read in a CSV file, update the 7th column of every row to be 50, save it to a new file (or over top the old if that's possible, doesn't matter for this example).  Below is most of the code for my failed attempt:

 

    public partial class Window1 : Window
    {
        private ICollection<DataRecord> records;
        private string filename;

        public Window1()
        {
            //removed for example because it was using a file dialog, etc.
            filename="a path to a known csv file";
            InitializeComponent();
        }

        private void saveCSV()
        {
            CsvWriter writer = new CsvWriter(filename);
            writer.WriteDataRecords(records);
            writer.Close();
        }

        private void modifyCSV()
        {
            foreach (DataRecord record in records)
            {
                record[7] = "50";
            }
        }

        private void loadCSV()
        {
            CsvReader reader = new CsvReader(newFn);
            records = reader.ReadDataRecords();
            reader.Close();
        }

        private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            loadCSV();
            modifyCSV();
            saveCSV();
        }
    }

It fails on the line that assigns "50" to the 7th column of the record.  You make reference to the 'Item' property on a DataRecord but I've not yet found out how to get to that.

Another thing I tried that I won't detail here is using the CsvReader to fill a DataTable, but you cannot do that without a header record hence the wording of my question.  Any suggestions?

 

 


 

Coordinator
Feb 8, 2010 at 11:31 AM

Hi Rick,

The easiest way to do this would be to read the original file as strings, modify the element(s) in question, and then write to the new file:

using (var csvReader = new CsvReader(fileToRead))
using (var csvWriter = new CsvWriter(fileToWrite))
{
    // don't want the header
    csvReader.SkipRecord();

    while (csvReader.HasMoreRecords)
    {
        var record = csvReader.ReadDataRecordAsStrings();
        record[7] = "50";
        csvWriter.WriteDataRecord(record);
    }
}

This bypasses the fact that the DataRecord class has immutable values.

HTH,
Kent