What I'm trying to do is parse a CSV file that has column headers, and map those headers - ideally to a class.
The tricky part is that I need to be able to support various mappings because it will be interacting with different systems that give the headers different names. Additionally, being able to combine fields would be extremely helpful.
Fortunately, the CSV will always be correctly formatted with all fields encapsulated in double quotes.
"Address", "City", "State", "Zip" "001 Test Rd", "New York", "New York", "10001"
"Street Number", "StreetName", "StreetSuffix" "City", "State", "Zip-5" "001", "Test", "Rd", "New York", "New York", "10001"
Public Class HouseInfo Private address as String Private city As String Private state As String Private zipcode As String End Class
I need to some way of mapping the header fields to properties in a class.
Below is simply a LONGsummary of the solutions that are on my mind; possibly worth looking into, etc. Only read below if you've got time to kill XD
- Perhaps XML/XSLT would work? Im not quite sure how, unfortunately my knowledge of XML/XSLT is very limited.
The XML would simply map the fields. Not sure how to implement it, but what I had in mind was something like the following.
<field> <property>Address</property> <header>[Street Number] [Street Name] [Street Suffix]</header> </field> <field> <property>City</property> <header>[City]</header> </field> etc
- Another notion I have is to use a DataSet and a DataAdapter that defines the schema.
Similar to the XML solution, I have very little knowledge of how to go about this solution, if it is even feasible. I've looked up numerous examples CSV + DataSet and haven't been able to find any that make use of the DataAdapter outside of Database oriented projects.
EditI tried using FileHelpers 2.0 a bit
It has a fairly nice and elegant implementation - creating a class and casting the FileHelper engine to that class so that the data becomes directly accessible via class properties. (IE: HouseInfo.Address)
Couldn't quite get the mapping that I am looking for though. Unless if Ive overlooked something, FileHelpers simply maps Column Indexs with Class Properties one after the other. Since it doesn't make use of the files headers, if the columns are moved around, the result is undesirable.
The example data/class is a *highly* simplified example. The real world application will be using over 100 fields across over a dozen different systems. That is why having an isolated Mapping / Schema solution would be ideal.
I've been racking my brain over how to do this for a few days now. I've come up with a few other solutions, but none of them are all too elegant or efficient.