Here is my data set which has come from a command which i have no control over the output:
BOOLEANCOLUMN NAME CLUSTER AUTHINFO
someName someCluster SomeMoreData
* name2 Data2 Data2
I want to be able to convert this into a class to work with the data, I am trying to use CSVHelper however they headers are not delimited, they are spaced to format the data to be human readable. I currently have a mapping class as so:
public ContextMap()
{
Map(m => m.Name).Name("NAME");
Map(m => m.AuthInfo).Name("AUTHINFO");
Map(m => m.Cluster).Name("CLUSTER");
Map(m => m.BooleanColumn).Name("BOOLEANCOLUMN");
}
but i am getting the exception :
CsvHelper.MissingFieldException: 'Field with name 'NAME' does not exist. You can ignore missing fields by setting MissingFieldFound to null.'
Question: How can I parse this data to a class?
I looked at the documentation and it seems the developer has got plans to implement a fixed width parser but hasn't made it yet.
To get around this issue, I created the following method to convert the data to delimited in code instead:
public static string ConvertFixedWidthToCsv(string data)
{
var rows = data.Split(
new[] { "\r\n", "\r", "\n" },
StringSplitOptions.None
);
var returnRows = new List<string>();
foreach (var row in rows)
{
if (string.IsNullOrWhiteSpace(row))
{
continue;
}
var parsed = Regex.Replace(row, @"\s+", ",");
returnRows.Add(parsed);
}
return String.Join("\r\n", returnRows);
}