/// <summary> /// Serialize an array of objects to CSV format /// </summary> /// <typeparam name="T">The type of objects to serialize from this CSV</typeparam> /// <param name="list">The array of objects to serialize</param> /// <param name="stream">The stream to which we will send this CSV text</param> /// <param name="settings">The CSV settings to use when exporting this array (Default: CSV)</param> /// <returns>The completed CSV string representing one line per element in list</returns> public static void Serialize <T>(IEnumerable <T> list, Stream stream, CSVSettings settings = null) where T : class, new() { using (var cw = new CSVWriter(stream, settings)) { cw.Serialize(list); } }
/// <summary> /// Serialize an array of objects to CSV format /// </summary> /// <typeparam name="T">The type of objects to serialize from this CSV</typeparam> /// <param name="list">The array of objects to serialize</param> /// <param name="settings">The CSV settings to use when exporting this array (Default: CSV)</param> /// <returns>The completed CSV string representing one line per element in list</returns> public static string Serialize <T>(IEnumerable <T> list, CSVSettings settings = null) where T : class, new() { if (settings == null) { settings = CSVSettings.CSV; } using (var ms = new MemoryStream()) { using (var cw = new CSVWriter(ms, settings)) { cw.Serialize(list); } var rawString = settings.Encoding.GetString(ms.ToArray()); return(RemoveByteOrderMarker(rawString)); } }