i'm trying read 2 types of records out of csv file following structure:
placename,longitude,latitude,elevation namestring,123.456,56.78,40 date,count 1/1/2012,1 2/1/2012,3 3/1/2012,10 4/2/2012,6
i know question has been covered in
but when run implementation gets csvmissingfieldexception
saying fields 'date' not exist in csv file
. have 2 definition , map classes, 1 location , other counts, are:
public class locationdefinition { public string placename { get; set; } public double longitude { get; set; } public double latitude { get; set; } public double elevation { get; set; } } public sealed class locationmap : csvclassmap<locationdefinition> { public locationmap() { map(m => m.placename).name("placename"); map(m => m.longitude).name("longitude"); map(m => m.latitude).name("latitude"); map(m => m.elevation).name("elevation"); } } public class countdefinition { public datetime date { get; set; } public int count { get; set; } } public sealed class countmap : csvclassmap<countdefinition> { public countmap() { map(m => m.date).name("date"); map(m => m.count).name("count"); } }
the code have reading csv file is:
locationdefinition location; var counts = new list<countdefinition>(); using (textreader filereader = file.opentext(@"path\to\csvfile")) using (var csvreader = new csvreader(filereader)) { csvreader.configuration.registerclassmap<locationmap>(); csvreader.configuration.registerclassmap<countmap>(); // reads single line of location data csvreader.read(); locationdata = csvreader.getrecord<locationdefinition>(); csvreader.read(); // skip blank line csvreader.read(); // skip second header section // read count data records while (csvreader.read()) { var tempcount = csvreader.getrecord<countdefinition>(); counts.add(tempcount); } }
the exception gets thrown on tempcount
line. can tell still expects location record, have thought getrecord<countdefinition>
specify record type. i've tried clearrecordcache
, unregistering locationmap
no avail.
how should code changed read csv file of structure?
try this
using system; using system.collections.generic; using system.linq; using system.text; using system.io; namespace consoleapplication1 { enum state { find_record, get_location, get_dates } class program { const string filename = @"c:\temp\test.txt"; static void main(string[] args) { streamreader reader = new streamreader(filename); state state = state.find_record; locationdefinition location = null; string inputline = ""; while ((inputline = reader.readline()) != null) { inputline = inputline.trim(); if (inputline.length == 0) { state = state.find_record; } else { switch (state) { case state.find_record : if (inputline.startswith("placename")) { state = state.get_location; } else { if (inputline.startswith("date")) { state = state.get_dates; } } break; case state.get_dates : if (location.dates == null) location.dates = new countdefinition(); location.dates.dates.add(new countdefinition(inputline)); break; case state.get_location : location = new locationdefinition(inputline); break; } } } } } public class locationdefinition { public static list<locationdefinition> locations = new list<locationdefinition>(); public countdefinition dates { get; set; } public string placename { get; set; } public double longitude { get; set; } public double latitude { get; set; } public double elevation { get; set; } public locationdefinition(string location) { string[] array = location.split(new char[] { ',' }, stringsplitoptions.removeemptyentries); placename = array[0]; longitude = double.parse(array[1]); latitude = double.parse(array[2]); elevation = double.parse(array[3]); locations.add(this); } } public class countdefinition { public list<countdefinition> dates = new list<countdefinition>(); public datetime date { get; set; } public int count { get; set; } public countdefinition() { ;} public countdefinition(string count) { string[] array = count.split(new char[] { ',' }, stringsplitoptions.removeemptyentries); date = datetime.parse(array[0]); count = int.parse(array[1]); dates.add(this); } } }
Comments
Post a Comment