public static Configuration GetConfiguration(string datatypeName) { var conf = new CsvHelper.Configuration.Configuration() { Delimiter = ",", HasHeaderRecord = false }; switch (datatypeName.ToLower()) { case "parametres": conf.Delimiter = "\t"; conf.RegisterClassMap <Parametres.ParametersMap>(); return(conf); case "data": conf.RegisterClassMap <Data.DataMap>(); return(conf); case "dataindicbruit": conf.RegisterClassMap <Dataindicbruit.DataindicbruitMap>(); return(conf); default: throw new Exception($"bad type name : the parsing of the type {datatypeName} is not implemented"); } }
public void MapMultipleTypesTest() { var config = new CsvHelper.Configuration.Configuration(); config.RegisterClassMap <AMap>(); config.RegisterClassMap <BMap>(); Assert.IsNotNull(config.Maps[typeof(A)]); Assert.IsNotNull(config.Maps[typeof(B)]); }
public void RegisterClassInstanceTest() { var config = new CsvHelper.Configuration.Configuration(); Assert.IsNull(config.Maps[typeof(TestClass)]); config.RegisterClassMap(new TestClassMappings()); Assert.IsNotNull(config.Maps[typeof(TestClass)]); }
public void RegisterClassMapNonGenericTest() { var config = new CsvHelper.Configuration.Configuration(); Assert.IsNull(config.Maps[typeof(TestClass)]); config.RegisterClassMap(typeof(TestClassMappings)); Assert.IsNotNull(config.Maps[typeof(TestClass)]); }
public void AddingMappingsWithInstanceMethodTest() { var config = new CsvHelper.Configuration.Configuration(); config.RegisterClassMap(new TestClassMappings()); Assert.AreEqual(2, config.Maps[typeof(TestClass)].MemberMaps.Count); }
public void AddingMappingsWithGenericMethod2Test() { var config = new CsvHelper.Configuration.Configuration(); config.RegisterClassMap <TestClassMappings>(); Assert.AreEqual(2, config.Maps[typeof(TestClass)].MemberMaps.Count); }
public void PropertyMapAccessTest() { var config = new CsvHelper.Configuration.Configuration(); config.RegisterClassMap <AMap>(); config.Maps.Find <A>().Map(m => m.AId).Ignore(); Assert.AreEqual(true, config.Maps[typeof(A)].MemberMaps[0].Data.Ignore); }
public virtual IEnumerable <T> GetData(string FilePath, Func <CsvReader, bool> filter) { IEnumerable <T> info = (IEnumerable <T>)null; //ExceptionList errors = new ExceptionList(); List <T> quotes = new List <T>(); try { _syncLock.EnterReadLock(); _config.ReadingExceptionOccurred = (ex) => HandleError(ex); //_config.BadDataCallback = (ex, row) => HandleError(ex, ref errors); if (_map != null) { _config.RegisterClassMap(_map); } else { _config.AutoMap <T>(); } using (TextReader textReader = File.OpenText(FilePath)) using (CsvReader reader = new CsvReader(textReader, _config)) { if (filter is null) { quotes.AddRange(reader.GetRecords <T>().ToList()); } else { while (reader.Read()) { if (filter(reader)) { quotes.Add(reader.GetRecord <T>()); } } } } } catch (Exception ex) { throw ex; //errors.AddOperationException($"CSV could get get info from {FilePath}.", FilePath, ex); } finally { _syncLock.ExitReadLock(); } //info = await Task.FromResult(quotes); //errors = await Task.FromResult(errors); return(quotes.AsEnumerable <T>()); }
private static void Initialize(string metadataFileName, string connectionString) { var metadataFileReader = new StreamReader(metadataFileName); var configuration = new CsvHelper.Configuration.Configuration() { HasHeaderRecord = false }; configuration.RegisterClassMap <RepositoryMetadataLogMap>(); _csvReader = new CsvReader(metadataFileReader, configuration); _context = new EntitiesContext(connectionString, readOnly: false); _cursor = new FileCursor(CursorFileName); _log = new Log(ErrorsFileName); }
/// <summary> /// Reads all records in file. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="filePath">The file path.</param> /// <param name="headerRecortExist">if set to <c>true</c> [header recort exist].</param> /// <param name="mappingClass">The mapping class.</param> /// <returns></returns> public static List <T> ReadAllRecords <T>(string filePath, bool headerRecortExist = false, Type mappingClass = null) { if (mappingClass != null) { configuration.RegisterClassMap(mappingClass); } using (FileStream fs = File.Open(filePath, FileMode.Open)) using (StreamReader sr = new StreamReader(fs)) using (CsvReader csv = new CsvReader(sr, configuration)) { csv.Read(); var records = csv.GetRecords <T>(); return(records.ToList()); } }
/// <summary> /// Queries the reporting endpoint with the specified filters and interpolated classes /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="U"></typeparam> /// <param name="reportingFilters"></param> /// <param name="betaEndPoint"></param> /// <returns></returns> private ICollection <T> QueryBetaOrCSVMap <T, U>(QueryFilter reportingFilters, bool betaEndPoint = false) where T : JSONResult where U : CSVConfig.ClassMap { var successOrWillTry = false; var results = new List <T>(); if (betaEndPoint) { // Switch to JSON Output try { reportingFilters.FormattedOutput = ReportUsageFormatEnum.JSON; var activityresults = ResponseReader.RetrieveData <T>(reportingFilters); results.AddRange(activityresults); successOrWillTry = true; } catch (Exception ex) { Logger.LogError(ex, $"Failed for JSON Format with message {ex.Message}"); } } if (!successOrWillTry) { // Switch to CSV Output reportingFilters.FormattedOutput = ReportUsageFormatEnum.Default; reportingFilters.BetaEndPoint = false; var CSVConfig = new CSVConfig.Configuration() { Delimiter = ",", HasHeaderRecord = true }; CSVConfig.RegisterClassMap <U>(); var resultscsv = new CSV.CsvReader(ResponseReader.RetrieveDataAsStream(reportingFilters), CSVConfig); results.AddRange(resultscsv.GetRecords <T>()); } Logger.LogInformation($"Found {results.Count} while querying successOrWillTry:{successOrWillTry}"); return(results); }
static void Main(string[] args) { //assuming the unique key is in the first column var config = new CsvHelper.Configuration.Configuration { HasHeaderRecord = false, IgnoreBlankLines = true, TrimOptions = TrimOptions.Trim, IgnoreQuotes = true }; config.RegisterClassMap <RecordDataLayoutMap>(); using (var fileReader = System.IO.File.OpenText(@"C:\temp\WaDEImportFiles\sites.csv")) using (var csvReader = new CsvHelper.CsvReader(fileReader, config)) { var allRecords = csvReader.GetRecords <RecordData>(); System.IO.File.WriteAllLines(@"C:\temp\WaDEImportFiles\sites_deduped.csv", allRecords.GroupBy(a => a.Key.ToLower()) .Select(a => a.First()) .OrderBy(a => a.RecordNumber) .Select(a => a.Record)); } }
protected override void ProcessRecord() { var sun = GenerateStar(); bool findsys; do { sun.BodyFormationScience = new Accrete(Ratio.FromDecimalFractions(CloudEccentricity), Ratio.FromDecimalFractions(GasDensityRatio), Ratio.FromDecimalFractions(DustDensityCoeff)); sun.GenerateSystem(CreatePlanet); findsys = (from p in sun.Satellites where p.IsHabitable select p).Count() > 0; } while (!findsys && OnlyHabitableSystem); if (String.IsNullOrEmpty(CsvOutputPath)) { WriteObject(sun); WriteObject(sun.Satellites); } else { var conf = new CsvHelper.Configuration.Configuration(); conf.RegisterClassMap <PlanetMap>(); var f = new FileStream(CsvOutputPath, FileMode.Create); var w = new StreamWriter(f); var cw = new CsvWriter(w, conf); cw.WriteHeader <SatelliteBody>(); cw.NextRecord(); cw.WriteRecords <SatelliteBody>(sun.Satellites); cw.Flush(); w.Close(); } }