Exemplo n.º 1
0
        public void ClearReaderTest()
        {
            using (var stream = new MemoryStream())
                using (var reader = new StreamReader(stream))
                    using (var writer = new StreamWriter(stream))
                        using (var csv = new CsvReader(reader))
                        {
                            writer.WriteLine("Id,Name");
                            writer.WriteLine("1,one");
                            writer.WriteLine("2,two");
                            writer.Flush();
                            stream.Position = 0;

                            csv.Configuration.RegisterClassMap <TestMap1>();
                            csv.Read();
                            var record = csv.GetRecord <Test>();

                            Assert.IsNotNull(record);
                            Assert.AreEqual(1, record.Id);
                            Assert.AreEqual(null, record.Name);

                            stream.Position = 0;
                            csv.ClearRecordCache <Test>();

                            csv.Configuration.RegisterClassMap <TestMap2>();
                            csv.Read();
                            record = csv.GetRecord <Test>();

                            Assert.IsNotNull(record);
                            Assert.AreEqual(0, record.Id);
                            Assert.AreEqual("two", record.Name);
                        }
        }
Exemplo n.º 2
0
        public static IList <T> GetEntitiesFromStream <T>(Stream stream, params CsvColumnMapping <T>[] additionalMapping) where T : class
        {
            IList <T> entities = new List <T>();

            using (StreamReader reader = new StreamReader(stream))
            {
                CsvReader csvReader = GetReader <T>(reader);

                var currentCulture = Thread.CurrentThread.CurrentCulture;
                Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

                while (csvReader.Read())
                {
                    T entity = null;
                    try
                    {
                        entity = csvReader.GetRecord <T>();
                    }
                    catch (CsvTypeConverterException ex)
                    {
                        throw new InvalidOperationException(
                                  string.Format(CultureInfo.InvariantCulture, "Error parsing file"), ex);
                    }
                    catch (FormatException ex)
                    {
                        throw new InvalidOperationException(
                                  string.Format(CultureInfo.InvariantCulture, "Error parsing file"), ex);
                    }

                    foreach (CsvColumnMapping <T> csvColumnMapping in additionalMapping)
                    {
                        if (csvColumnMapping.Culture != null)
                        {
                            Thread.CurrentThread.CurrentCulture = csvColumnMapping.Culture;
                            csvReader.Configuration.CultureInfo = csvColumnMapping.Culture;
                        }
                        else
                        {
                            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
                            csvReader.Configuration.CultureInfo = CultureInfo.InvariantCulture;
                        }

                        csvReader.ClearRecordCache();

                        var record = csvReader.GetRecord <T>();

                        csvColumnMapping.Execute(ref entity, csvReader.GetField(csvColumnMapping.CsvColumnName));
                    }

                    entities.Add(entity);
                }

                Thread.CurrentThread.CurrentCulture = currentCulture;
            }

            return(entities);
        }
Exemplo n.º 3
0
        public void DeleteAnimal(Animal animal)
        {
            var newAnimals = new List <Animal>();

            using (TextReader fileReader = File.OpenText(Path))
                using (var csv = new CsvReader(fileReader))
                {
                    csv.Read();

                    var records = csv.GetRecords <Animal>();
                    foreach (var r in records)
                    {
                        if (r.Name != animal.Name)
                        {
                            newAnimals.Add(r);
                        }
                    }

                    csv.ClearRecordCache();
                }


            System.IO.File.Delete(Path);

            using (FileStream fstream = System.IO.File.Create(Path))
            {
                string s1     = "Id,Name,Age,Class,Gender";
                byte[] array  = System.Text.Encoding.Default.GetBytes(s1);
                string s2     = "0,0,0,0,0";
                byte[] array2 = System.Text.Encoding.Default.GetBytes(s2);
                byte[] array3 = System.Text.Encoding.Default.GetBytes("\n");
                fstream.Write(array, 0, array.Length);
                fstream.Write(array3, 0, array3.Length);
                fstream.Write(array2, 0, array2.Length);
                fstream.Write(array3, 0, array3.Length);
            }


            var config = new CsvConfiguration()
            {
                HasHeaderRecord = false,
                Delimiter       = ","
            };

            using (var stream = File.Open(Path, FileMode.Append))
                using (var writer = new StreamWriter(stream))
                    using (var csvw = new CsvWriter(writer, config))
                    {
                        csvw.WriteRecords(newAnimals);
                    }
        }