示例#1
0
        private void LoadCsvButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var dialog = new Microsoft.Win32.OpenFileDialog();
                dialog.InitialDirectory = string.IsNullOrEmpty(CSVFile.Text) ?
                                          "c:\\" : Path.GetDirectoryName(CSVFile.Text);
                dialog.FileName = CSVFile.Text;
                if (dialog.ShowDialog() != true)
                {
                    return;
                }

                using (var fs = File.OpenRead(dialog.FileName))
                    using (var sr = new StreamReader(fs, Encoding.Default))
                    {
                        var csv = new CsvReaderService <Employee>(sr);
                        csv.Configuration.BlankRowsAreReturnedAsNull = true;

                        while (csv.CanRead())
                        {
                            Employee record = csv.GetRecord();
                            LogMessage(record.ToString());
                        }
                    }
            }
            catch (Exception ex)
            {
                LogError(ex);
            }
        }
示例#2
0
        private void CustomConverterLoadCsvButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var dialog = LoadFile(CustomConverterCSVFile.Text);
                if (dialog.ShowDialog() != true)
                {
                    return;
                }

                using (var fs = File.OpenRead(dialog.FileName))
                    using (var sr = new StreamReader(fs, Encoding.Default))
                    {
                        ICsvReaderService <Person> reader = new CsvReaderService <Person>(sr);
                        reader.Configuration.BlankRowsAreReturnedAsNull = true;

                        while (reader.CanRead())
                        {
                            Person record = reader.GetRecord();
                            if (record != null)
                            {
                                LogMessage(record.ToString());
                            }
                        }
                    }
            }
            catch (Exception ex)
            {
                LogError(ex);
            }
        }
示例#3
0
        public void GetRecord_BlankRowsAreReturnedAsNull_BlankRowsIgnored()
        {
            var tempFileName = Path.GetTempFileName();

            try
            {
                using (var fs = File.Create(tempFileName))
                    using (var sw = new StreamWriter(fs))
                    {
                        sw.WriteLine("Name,Age");
                        sw.WriteLine("Fido,1");
                        sw.WriteLine(",");
                        sw.WriteLine("");
                        sw.WriteLine("Killer,2");
                        sw.WriteLine("Jaws,3");
                        sw.WriteLine("   ");
                        sw.WriteLine("Fluffy,4");
                        sw.WriteLine("John the dog,5");
                        sw.WriteLine("   ");
                    }

                var result = new List <Dog>();
                using (var fs = File.OpenRead(tempFileName))
                    using (var sr = new StreamReader(fs))
                    {
                        var reader = new CsvReaderService <Dog>(sr);
                        reader.Configuration.HasHeaderRow = true;
                        reader.Configuration.BlankRowsAreReturnedAsNull = true; // HERE IS WHAT WE ARE TESTING!!
                        while (reader.CanRead())
                        {
                            var data = reader.GetRecord();
                            if (data != null)
                            {
                                result.Add(data);
                            }
                        }
                    }

                Assert.AreEqual(5, result.Count, "Expecting only 5 dogs!");
            }
            catch
            {
                File.Delete(tempFileName);
                throw;
            }
        }
示例#4
0
        public void GetRecord_CanHandelClassWithOneProperty_DataRead(int expectedNumberOfRows, bool blankRowsAreReturnedAsNull)
        {
            var tempFileName = Path.GetTempFileName();

            try
            {
                using (var fs = File.Create(tempFileName))
                    using (var sw = new StreamWriter(fs))
                    {
                        sw.WriteLine("Name");
                        sw.WriteLine("George");
                        sw.WriteLine("");
                        sw.WriteLine("Bob");
                        sw.WriteLine("Tom");
                        sw.WriteLine("   ");
                        sw.WriteLine("Ralph");
                        sw.WriteLine("James");
                        sw.WriteLine("   ");
                    }

                var result = new List <ClassWithOneProperty>();
                using (var fs = File.OpenRead(tempFileName))
                    using (var sr = new StreamReader(fs))
                    {
                        var reader = new CsvReaderService <ClassWithOneProperty>(sr);
                        reader.Configuration.HasHeaderRow = true;
                        reader.Configuration.BlankRowsAreReturnedAsNull = blankRowsAreReturnedAsNull;
                        while (reader.CanRead())
                        {
                            var data = reader.GetRecord();
                            if (data != null || blankRowsAreReturnedAsNull == false)
                            {
                                result.Add(data);
                            }
                        }
                    }

                Assert.AreEqual(expectedNumberOfRows, result.Count, $"Expecting {expectedNumberOfRows} names!");
            }
            catch
            {
                File.Delete(tempFileName);
                throw;
            }
        }
示例#5
0
 private static void ReadItems(string fileName)
 {
     using (var fs = File.OpenRead(fileName))
         using (var sr = new StreamReader(fs))
         {
             ICsvReaderService <TestData> reader = new CsvReaderService <TestData>(sr);
             reader.Configuration.HasHeaderRow = true;
             reader.Configuration.BlankRowsAreReturnedAsNull = true;
             while (reader.CanRead())
             {
                 TestData item = reader.GetRecord();
                 if (item != null)
                 {
                     Console.WriteLine(item.FieldDescription);
                 }
             }
         }
 }
        public void CanReadAndWriteData()
        {
            string fileName = GetTestFileNameAndPath();

            var dataList = new List <OddlyShapedData>();

            using (var fs = File.OpenRead(fileName))
                using (var sr = new StreamReader(fs, Encoding.Default))
                {
                    var csv = new CsvReaderService <OddlyShapedData>(sr);
                    csv.Configuration.HasHeaderRow = true;
                    csv.Configuration.BlankRowsAreReturnedAsNull         = true;
                    csv.Configuration.ThrowExceptionIfColumnCountChanges = false;

                    while (csv.CanRead())
                    {
                        dataList.Add(csv.GetRecord());
                    }
                }

            OddlyShapedData data1 = dataList.FirstOrDefault(w => w.FirstName == "David");

            Assert.IsNotNull(data1, "Could not find record with David Jackson");
            Assert.AreEqual("Jackson", data1.LastName);
            Assert.AreEqual(45, data1.Age);
            Assert.AreEqual(72, data1.HeightInInches);

            OddlyShapedData data2 = dataList.FirstOrDefault(w => w.FirstName == "Heather");

            Assert.IsNotNull(data2, "Could not find record with Heather Thomas");
            Assert.AreEqual("Thomas", data2.LastName);
            Assert.AreEqual(0, data2.Age);
            Assert.AreEqual(0, data2.HeightInInches);


            OddlyShapedData data3 = dataList.FirstOrDefault(w => w.FirstName == "Janet");

            Assert.IsNotNull(data3, "Could not find record with Janet Reno");
            Assert.AreEqual("Reno", data3.LastName);
            Assert.AreEqual(65, data3.Age);
            Assert.AreEqual(70, data3.HeightInInches);
        }
示例#7
0
        private static List <Squirrel> ReadSquirrels(string squirrelFileName)
        {
            var result = new List <Squirrel>();

            using (var fs = File.OpenRead(squirrelFileName))
                using (var sr = new StreamReader(fs))
                {
                    ICsvReaderService <Squirrel> reader = new CsvReaderService <Squirrel>(sr);
                    reader.Configuration.HasHeaderRow = true;
                    reader.Configuration.BlankRowsAreReturnedAsNull = true;
                    while (reader.CanRead())
                    {
                        Squirrel item = reader.GetRecord();
                        if (item != null)
                        {
                            result.Add(item);
                        }
                    }
                }

            return(result);
        }
示例#8
0
        public void CanReadAndWriteData()
        {
            string fileName = GetTestFileNameAndPath();

            var studentList = new List <Graduation>();

            using (var fs = File.OpenRead(fileName))
                using (var sr = new StreamReader(fs, Encoding.Default))
                {
                    var csv = new CsvReaderService <Graduation>(sr);
                    csv.Configuration.HasHeaderRow = true;
                    csv.Configuration.BlankRowsAreReturnedAsNull = true;

                    while (csv.CanRead())
                    {
                        studentList.Add(csv.GetRecord());
                    }
                }

            // Create a temp file
            string tempFileName = Path.Combine(
                Path.GetDirectoryName(fileName),
                Path.GetFileNameWithoutExtension(fileName) + "Temp.csv");

            using (var fs = File.Create(tempFileName))
                using (var sw = new StreamWriter(fs, Encoding.Default))
                {
                    var service = new CsvWriterService <Graduation>(sw);
                    service.Configuration.HasHeaderRow = true;
                    foreach (var student in studentList)
                    {
                        service.WriteRecord(student);
                    }
                }

            Assert.IsTrue(FilesAreEqual(fileName, tempFileName));
        }
        private void LoadFileButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var dialog = LoadFile("c:\\temp\\Person.csv");
                if (dialog.ShowDialog() != true)
                {
                    return;
                }

                LogMessage("Before overriding");
                using (var fs = File.OpenRead(dialog.FileName))
                    using (var sr = new StreamReader(fs))
                    {
                        ICsvReaderService <Person> reader = new CsvReaderService <Person>(sr);

                        while (reader.CanRead())
                        {
                            var person = reader.GetRecord();
                            LogMessage(person.ToString());
                        }
                    }

                LogMessage("After overriding");
                using (var fs = File.OpenRead(dialog.FileName))
                    using (var sr = new StreamReader(fs))
                    {
                        // Keep in mind that your replacement converter must be standalone.
                        // You cannot rely in any default converters.
                        // Default converters created when the service class is created.
                        ICsvReaderService <Person> reader = new CsvReaderService <Person>(sr);

                        // Replace a converter
                        reader.DefaultConverterFactory.ReplaceConverter(typeof(string), typeof(CsvConverterStringTextLengthEnforcer));

                        // Create all the column mappings.
                        reader.Init();

                        #region This converter has settings different that the defaults when it was constructed
                        // Well, you will have to find the columns with the converter that need changing

                        // We could just find string properties since it is the default, but that is problematic.
                        // What if a class property, which is a string, has a custom converter on it?
                        // So, let's just find the properties that are using our new default converter.
                        List <ColumnToPropertyMap> columnList = reader.FindColumnsByConverterType(typeof(CsvConverterStringTextLengthEnforcer));

                        foreach (ColumnToPropertyMap oneColumn in columnList)
                        {
                            if (oneColumn.ReadConverter is CsvConverterStringTextLengthEnforcer converter)
                            {
                                converter.MinimumLength = 5;
                                converter.MaximumLength = 6;
                                converter.CharacterToAddToShortStrings = '-';
                            }
                        }
                        #endregion

                        while (reader.CanRead())
                        {
                            var person = reader.GetRecord();
                            LogMessage(person.ToString());
                        }
                    }
            }
            catch (Exception ex)
            {
                LogError(ex);
            }
        }