Exemple #1
0
    /// <summary>
    /// Reads the CSV file as Records
    /// </summary>
    /// <param name="filePath">The file path.</param>
    /// <param name="hasHeader">If true, has header.</param>
    /// <param name="headerValidated"></param>
    /// <param name="delimiter">The delimiter.</param>
    /// <returns>A list of TS.</returns>
    public static IEnumerable <T> ReadCsv <T>(
        this string filePath,
        bool hasHeader = true,
        HeaderValidated headerValidated = null,
        string delimiter = ","
        )
    {
        if (!File.Exists(filePath))
        {
            Log.Information("File {FilePath} is not exist", filePath);
            return(new List <T>());
        }

        var csvConfiguration = new CsvConfiguration(CultureInfo.CurrentCulture)
        {
            HasHeaderRecord       = hasHeader,
            Delimiter             = delimiter,
            MissingFieldFound     = null,
            BadDataFound          = null,
            HeaderValidated       = headerValidated,
            PrepareHeaderForMatch = (header) => header.Header.ToLower()
        };

        using var reader = new StreamReader(filePath);
        using var csv    = new CsvReader(reader, csvConfiguration);

        var records = csv.GetRecords <T>().ToList();

        Log.Information("Parsing csv records {Count} record(s)", records.Count);

        return(records);
    }
Exemple #2
0
 private void RaiseHeaderValidated()
 {
     HeaderValidated?.Invoke(this, new EventArgs());
 }
        public void Constructor_AllArguments_SetsProperites()
        {
            BadDataFound       badDataFound        = (context) => { };
            IComparer <string> dynamicPropertySort = Comparer <string> .Create((x, y) => x.CompareTo(y));

            GetConstructor           getConstructor           = (type) => type.GetConstructor(null);
            GetDynamicPropertyName   getDynamicPropertyName   = (context, field) => string.Empty;
            HeaderValidated          headerValidated          = (invalidHeaders, context) => { };
            MissingFieldFound        missingFieldFound        = (headerNames, index, context) => { };
            PrepareHeaderForMatch    prepareHeaderForMatch    = (header, fieldIndex) => header;
            ReadingExceptionOccurred readingExceptionOccurred = (ex) => true;
            ReferenceHeaderPrefix    referenceHeaderPrefix    = (type, memberName) => string.Empty;
            ShouldQuote      shouldQuote      = (_, _, _) => true;
            ShouldSkipRecord shouldSkipRecord = (record) => true;
            ShouldUseConstructorParameters shouldUseConstructorParameters = (parameterType) => true;

            var config = new CsvConfiguration(CultureInfo.CurrentCulture,
                                              allowComments: true,
                                              badDataFound: badDataFound,
                                              bufferSize: 1,
                                              cacheFields: true,
                                              comment: '^',
                                              countBytes: true,
                                              delimiter: ":",
                                              detectColumnCountChanges: true,
                                              dynamicPropertySort: dynamicPropertySort,
                                              encoding: Encoding.ASCII,
                                              escape: '\\',
                                              getConstructor: getConstructor,
                                              getDynamicPropertyName: getDynamicPropertyName,
                                              hasHeaderRecord: false,
                                              headerValidated: headerValidated,
                                              ignoreBlankLines: false,
                                              ignoreReferences: true,
                                              includePrivateMembers: true,
                                              injectionCharacters: new char[] { '*' },
                                              injectionEscapeCharacter: '`',
                                              leaveOpen: true,
                                              lineBreakInQuotedFieldIsBadData: true,
                                              memberTypes: MemberTypes.Fields,
                                              missingFieldFound: missingFieldFound,
                                              mode: CsvMode.Escape,
                                              newLine: "\n",
                                              prepareHeaderForMatch: prepareHeaderForMatch,
                                              quote: '\'',
                                              readingExceptionOccurred: readingExceptionOccurred,
                                              referenceHeaderPrefix: referenceHeaderPrefix,
                                              sanitizeForInjection: true,
                                              shouldQuote: shouldQuote,
                                              shouldSkipRecord: shouldSkipRecord,
                                              shouldUseConstructorParameters: shouldUseConstructorParameters,
                                              trimOptions: TrimOptions.InsideQuotes,
                                              useNewObjectForNullReferenceMembers: false,
                                              whiteSpaceChars: new char[] { '~' }
                                              );

            Assert.IsTrue(config.AllowComments);
            Assert.AreEqual(badDataFound, config.BadDataFound);
            Assert.AreEqual(1, config.BufferSize);
            Assert.IsTrue(config.CacheFields);
            Assert.AreEqual('^', config.Comment);
            Assert.IsTrue(config.CountBytes);
            Assert.AreEqual(CultureInfo.CurrentCulture, config.CultureInfo);
            Assert.AreEqual(":", config.Delimiter);
            Assert.IsTrue(config.DetectColumnCountChanges);
            Assert.AreEqual(dynamicPropertySort, config.DynamicPropertySort);
            Assert.AreEqual(Encoding.ASCII, config.Encoding);
            Assert.AreEqual('\\', config.Escape);
            Assert.AreEqual(getConstructor, config.GetConstructor);
            Assert.AreEqual(getDynamicPropertyName, config.GetDynamicPropertyName);
            Assert.IsFalse(config.HasHeaderRecord);
            Assert.AreEqual(headerValidated, config.HeaderValidated);
            Assert.IsFalse(config.IgnoreBlankLines);
            Assert.IsTrue(config.IgnoreReferences);
            Assert.IsTrue(config.IncludePrivateMembers);
            Assert.AreEqual('*', config.InjectionCharacters[0]);
            Assert.AreEqual('`', config.InjectionEscapeCharacter);
            Assert.IsTrue(config.IsNewLineSet);
            Assert.IsTrue(config.LeaveOpen);
            Assert.IsTrue(config.LineBreakInQuotedFieldIsBadData);
            Assert.AreEqual(MemberTypes.Fields, config.MemberTypes);
            Assert.AreEqual(missingFieldFound, config.MissingFieldFound);
            Assert.AreEqual(CsvMode.Escape, config.Mode);
            Assert.AreEqual("\n", config.NewLine);
            Assert.AreEqual(prepareHeaderForMatch, config.PrepareHeaderForMatch);
            Assert.AreEqual('\'', config.Quote);
            Assert.AreEqual(readingExceptionOccurred, config.ReadingExceptionOccurred);
            Assert.AreEqual(referenceHeaderPrefix, config.ReferenceHeaderPrefix);
            Assert.IsTrue(config.SanitizeForInjection);
            Assert.AreEqual(shouldQuote, config.ShouldQuote);
            Assert.AreEqual(shouldSkipRecord, config.ShouldSkipRecord);
            Assert.AreEqual(shouldUseConstructorParameters, config.ShouldUseConstructorParameters);
            Assert.AreEqual(TrimOptions.InsideQuotes, config.TrimOptions);
            Assert.IsFalse(config.UseNewObjectForNullReferenceMembers);
            Assert.AreEqual('~', config.WhiteSpaceChars[0]);
        }