c# - Importing a multiline record with optional fields using filehelpers -
i'm in process of needing parse file who's records of following format:
mr sean r. farrow 4 crescent eastleake loughborough leicestershire le12 6qh 01509 59213 07525945447 sean.farrow@seanfarrow.co.uk
each record delimited blank line finish. 2 phone numbers , email address optional.
what best way of parsing sort of record? write own parser, hoping don't have to!
filehelpers expects each record end new line, you'd have pre-parse input before passing engine. that's straightforward though - like:
var lines = file.readalllines(pathtoimportfile); var sb = new stringbuilder(); var separator = ","; // use comma field delimiter foreach (string line in lines) { if (string.isnullorempty(line)) sb.appendline(""); // convert empty lines line feeds else sb.appendformat("\"{0}\"{1}", line, separator); // put quotes around field avoid problems nested separators } var engine = new filehelperengine<myclass>(); engine.readstring(sb.tostring());
and class like
[delimitedrecord(",")] class myclass { [fieldquoted(quotemode.alwaysquoted)] public string title; [fieldquoted(quotemode.alwaysquoted)] public string fullname; [fieldquoted(quotemode.alwaysquoted)] public string address1; /// ... etc }
Comments
Post a Comment