public void TestFormatValues() { var model = new Model() { field1 = 1, field2 = "field2 value" }; var formatter = new CsvFormatter(); var buffer = new StringBuilder(); buffer.Append(formatter.FormatItem(model)); Assert.AreEqual("1,field2 value", buffer.ToString()); // With a comma buffer = new StringBuilder(); model = new Model() { field1 = 1, field2 = "field2,value" }; buffer.Append(formatter.FormatItem(model)); Assert.AreEqual("1,\"field2,value\"", buffer.ToString()); }
private static string GenerateCSV(Device[] data) { var formatter = new CsvFormatter(); var buffer = new StringBuilder(); buffer.Append(formatter.FormatTitle(typeof(Device))); buffer.Append("\n"); foreach (Device item in data) { try { buffer.Append(formatter.FormatItem(item) + '\n'); } catch (Exception e) { Console.WriteLine($"Buffer Append Failed: {item} ({e.InnerException})"); } } return(buffer.ToString()); }