public void TestWriteRecord1([Values(EndOfLine.CRLF, EndOfLine.LF, EndOfLine.CR)] EndOfLine eol) { const string ascii = "ASCII"; const string nonAscii = "日本語"; const string empty = ""; const string withNewline = "hello\r\nworld"; var record = new[] { ascii, nonAscii, empty, withNewline }; var settings = new CsvWriterSettings() { RecordDelimiter = eol, }; using (var stream = new MemoryStream()) using (var streamWriter = new StreamWriter(stream)) using (var writer = new CsvWriter(streamWriter, settings) { AutoFlush = true }) { writer.WriteRecordRaw(record); stream.Seek(0, SeekOrigin.Begin); using (var reader = new StreamReader(stream)) { var expected = string.Join(",", record.Select(value => "\"" + value + "\"")) + eol.AsNewline(); var actual = reader.ReadToEnd(); Assert.That(actual, Is.EqualTo(expected)); } } }
public void TestWriteMultipleRecord([Values(EndOfLine.CRLF, EndOfLine.LF, EndOfLine.CR)] EndOfLine eol) { var record = new MultipleColumnRecord { Value = 1 }; var settings = new CsvWriterSettings() { RecordDelimiter = eol, }; using (var stream = new MemoryStream()) using (var streamWriter = new StreamWriter(stream)) using (var writer = new CsvWriter(streamWriter, settings) { AutoFlush = true }) { writer.WriteRecord(record); stream.Seek(0, SeekOrigin.Begin); using (var reader = new StreamReader(stream)) { var line = reader.ReadLine(); Assert.That(line, Is.Not.Null); var actual = line.Split(','); Assert.That(actual.Length, Is.EqualTo(2)); Assert.That(actual[0], Is.EqualTo("\"1\"")); Assert.That(actual[1], Is.EqualTo("\"一\"")); } } }
public void TestWriteIndexerRecord([Values(EndOfLine.CRLF, EndOfLine.LF, EndOfLine.CR)] EndOfLine eol) { var record = new IndexerRecord(); record["A"] = 1; record["B"] = 2; record["C"] = 3; var settings = new CsvWriterSettings() { RecordDelimiter = eol, }; using (var stream = new MemoryStream()) using (var streamWriter = new StreamWriter(stream)) using (var writer = new CsvWriter(streamWriter, settings) { AutoFlush = true }) { writer.WriteRecord(record); stream.Seek(0, SeekOrigin.Begin); using (var reader = new StreamReader(stream)) { var line = reader.ReadLine(); Assert.That(line, Is.Not.Null); var actual = line.Split(','); Assert.That(actual.Length, Is.EqualTo(3)); Assert.That(actual[0], Is.EqualTo("\"1\"")); Assert.That(actual[1], Is.EqualTo("\"二\"")); Assert.That(actual[2], Is.EqualTo("\"3\"")); } } }
public void TestWriteRecord2([Values(EndOfLine.CRLF, EndOfLine.LF, EndOfLine.CR)] EndOfLine eol) { var record = new FullRecord { IntField = 3, IntProperty = -1, NullableDoubleField = null, NullableDoubleProperty = 1.5, ParsedField = 1, ParsedProperty = null, StringField = "hello", StringProperty = null, }; var settings = new CsvWriterSettings { RecordDelimiter = eol, }; using (var stream = new MemoryStream()) using (var streamWriter = new StreamWriter(stream)) using (var writer = new CsvWriter(streamWriter, settings) { AutoFlush = true }) { writer.WriteRecord(record); stream.Seek(0, SeekOrigin.Begin); using (var reader = new StreamReader(stream)) { var line = reader.ReadLine(); Assert.That(line, Is.Not.Null); var actual = line.Split(','); Assert.That(actual.Length, Is.EqualTo(8)); Assert.That(actual[0], Is.EqualTo("\"hello\"")); Assert.That(actual[1], Is.EqualTo("\"3\"")); Assert.That(actual[6], Is.EqualTo("\"1.5\"")); Assert.That(actual[7], Is.EqualTo("\"NA(Int32)\"")); var restActual = new HashSet <string> { actual[2], actual[3], actual[4], actual[5] }; var restExpected = new HashSet <string> { "\"NA\"", "\"一\"", "\"\"", "\"-1\"" }; Assert.That(restActual.SetEquals(restExpected), Is.True); } } }
private CsvWriter(TextWriter writer, CsvWriterSettings settings, bool disposed) { if (writer == null) { throw new ArgumentNullException("writer"); } this.writer = writer; settings = settings ?? new CsvWriterSettings(); this.delimiter = settings.RecordDelimiter.AsNewline(); this.separator = settings.FieldDelimiter; this.quotation = settings.QuotationCharacter; this.quotationString = settings.QuotationCharacter.ToString(); this.quoteField = settings.QuoteField; this.escapedQuotation = this.quotationString + this.quotationString; this.disposed = disposed; }
public void TestWriteArrayRecord([Values(EndOfLine.CRLF, EndOfLine.LF, EndOfLine.CR)] EndOfLine eol) { var record = new ArrayRecord { Property = new[, ] { { 1, 2 }, { 3, 4 }, { 5, 6 } }, Field = new[] { 1, 2 }, }; var settings = new CsvWriterSettings() { RecordDelimiter = eol, }; using (var stream = new MemoryStream()) using (var streamWriter = new StreamWriter(stream)) using (var writer = new CsvWriter(streamWriter, settings) { AutoFlush = true }) { writer.WriteRecord(record); stream.Seek(0, SeekOrigin.Begin); using (var reader = new StreamReader(stream)) { var line = reader.ReadLine(); Assert.That(line, Is.Not.Null); var actual = line.Split(','); Assert.That(actual.Length, Is.EqualTo(8)); Assert.That(actual[0], Is.EqualTo("\"1\"")); Assert.That(actual[1], Is.EqualTo("\"2\"")); Assert.That(actual[2], Is.EqualTo("\"3\"")); Assert.That(actual[3], Is.EqualTo("\"4\"")); Assert.That(actual[4], Is.EqualTo("\"5\"")); Assert.That(actual[5], Is.EqualTo("\"6\"")); Assert.That(actual[6], Is.EqualTo("\"一\"")); Assert.That(actual[7], Is.EqualTo("\"二\"")); } } }
/// <summary> /// Initializes a new <see cref="CsvWriter"/> with the specified path to output. /// </summary> /// <param name="writer">The writer.</param> /// <param name="settings">The settings.</param> public CsvWriter(TextWriter writer, CsvWriterSettings settings = null) : this(writer, settings, true) { }
/// <summary> /// Initializes a new <see cref="CsvWriter"/> with the specified file stream. /// </summary> /// <param name="stream">The file stream.</param> /// <param name="encoding">The encoding.</param> /// <param name="settings">The settings.</param> public CsvWriter(Stream stream, Encoding encoding, CsvWriterSettings settings = null) : this(new StreamWriter(stream, encoding), settings, false) { }
/// <summary> /// Initializes a new <see cref="CsvWriter"/> with the specified file stream. /// </summary> /// <param name="stream">The file stream.</param> /// <param name="settings">The settings.</param> public CsvWriter(Stream stream, CsvWriterSettings settings = null) : this(new StreamWriter(stream), settings, false) { }
/// <summary> /// Initializes a new <see cref="CsvWriter"/> with the specified path to output. /// </summary> /// <param name="path">The path to file to write in.</param> /// <param name="encoding">The encoding.</param> /// <param name="settings">The settings.</param> public CsvWriter(string path, Encoding encoding, CsvWriterSettings settings = null) : this(new StreamWriter(path, false, encoding, 0x400), settings, false) { }
/// <summary> /// Initializes a new <see cref="CsvWriter"/> with the specified path to output. /// </summary> /// <param name="path">The path to file to write in.</param> /// <param name="settings">The settings.</param> public CsvWriter(string path, CsvWriterSettings settings = null) : this(new StreamWriter(path), settings, false) { }