public void TestReaderMapPropertiesWitoutHeaderRow() { using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) using (var reader = new StreamReader(stream)) using (var dtReader = new DelimitedTextReader(reader, '|')) { writer.Write("value1|100|true|\"12/31/2016\"| 25.76\r\n"); writer.Flush(); stream.Position = 0; dtReader.FirstRowIsHeader = false; dtReader.MapProperty <TestRecord>(m => m.Field1).MappedColumnIndex = 0; dtReader.MapProperty <TestRecord>(m => m.Field4).MappedColumnIndex = 3; dtReader.MapProperty <TestRecord>(m => m.Field2).MappedColumnIndex = 1; dtReader.MapProperty <TestRecord>(m => m.Field5).MappedColumnIndex = 4; dtReader.MapProperty <TestRecord>(m => m.Field3).MappedColumnIndex = 2; dtReader.Read(); TestRecord trecord = dtReader.GetRecord <TestRecord>(); Assert.NotNull(trecord); Assert.Equal("value1", trecord.Field1); Assert.Equal(100, trecord.Field2); Assert.True(trecord.Field3); Assert.Equal(DateTime.Parse("12/31/2016").ToShortDateString(), trecord.Field4.ToShortDateString()); Assert.Equal(25.76M, trecord.Field5); } }
public void TestReaderMapPropertiesWithFunkyDate() { using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) using (var reader = new StreamReader(stream)) using (var dtReader = new DelimitedTextReader(reader)) { writer.Write("Field1,Field2,Field3,Field4,Field5\r\n"); writer.Write("value1,100,true,\"20161231\", 25.76\r\n"); writer.Flush(); stream.Position = 0; dtReader.FirstRowIsHeader = true; dtReader.MapProperty <TestRecord>(m => m.Field1).ColumnIndex(0); dtReader.MapProperty <TestRecord>(m => m.Field2).ColumnIndex(1); dtReader.MapProperty <TestRecord>(m => m.Field3).ColumnIndex(2); dtReader.MapProperty <TestRecord>(m => m.Field4).ColumnIndex(3).TypeConverter(new DateTimeConverter() { Format = "yyyyMMdd" }); dtReader.MapProperty <TestRecord>(m => m.Field5).ColumnIndex(4); dtReader.Read(); TestRecord trecord = dtReader.GetRecord <TestRecord>(); Assert.NotNull(trecord); Assert.Equal("value1", trecord.Field1); Assert.Equal(100, trecord.Field2); Assert.True(trecord.Field3); Assert.Equal(DateTime.Parse("12/31/2016").ToShortDateString(), trecord.Field4.ToShortDateString()); Assert.Equal(25.76M, trecord.Field5); } }
public void TestReaderMapPropertiesByName() { using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) using (var reader = new StreamReader(stream)) using (var dtReader = new DelimitedTextReader(reader, '|')) { writer.Write("X1|X2|X3|X4|X5\r\n"); writer.Write("value1|100|true|\"12/31/2016\"| 25.76\r\n"); writer.Flush(); stream.Position = 0; dtReader.FirstRowIsHeader = true; dtReader.MapProperty <TestRecord>(m => m.Field1).ColumnName("X1"); dtReader.MapProperty <TestRecord>(m => m.Field4).ColumnName("X4"); dtReader.MapProperty <TestRecord>(m => m.Field2).ColumnName("X2"); dtReader.MapProperty <TestRecord>(m => m.Field5).ColumnName("X5"); dtReader.MapProperty <TestRecord>(m => m.Field3).ColumnName("X3"); dtReader.Read(); TestRecord trecord = dtReader.GetRecord <TestRecord>(); Assert.NotNull(trecord); Assert.Equal("value1", trecord.Field1); Assert.Equal(100, trecord.Field2); Assert.True(trecord.Field3); Assert.Equal(DateTime.Parse("12/31/2016").ToShortDateString(), trecord.Field4.ToShortDateString()); Assert.Equal(25.76M, trecord.Field5); } }
public void TestReaderMapPropertiesByUnknownName() { using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) using (var reader = new StreamReader(stream)) using (var dtReader = new DelimitedTextReader(reader, '|')) { writer.Write("X1|X2|X3|X4|X5\r\n"); writer.Write("value1|100|true|\"12/31/2016\"| 25.76\r\n"); writer.Flush(); stream.Position = 0; dtReader.FirstRowIsHeader = true; dtReader.MapProperty <TestRecord>(m => m.Field1).ColumnName("X1"); dtReader.MapProperty <TestRecord>(m => m.Field4).ColumnName("FOO"); dtReader.MapProperty <TestRecord>(m => m.Field2).ColumnName("X2"); dtReader.MapProperty <TestRecord>(m => m.Field5).ColumnName("X5"); dtReader.MapProperty <TestRecord>(m => m.Field3).ColumnName("X3"); dtReader.Read(); TestRecord trecord; Assert.Throws <DelimitedTextReaderMappingException>(() => trecord = dtReader.GetRecord <TestRecord>()); } }