Validate content of csv file c#

后端 未结 4 1737

I have a requirement where user will be uploading a csv file in the below format which will contain around 1.8 to 2 million records

SITE_ID,HOUSE,STREET,CITY         


        
4条回答
  •  时光取名叫无心
    2020-12-06 21:08

    Here is another way to accomplish your needs using Cinchoo ETL - an open source file helper library.

    First define a POCO class with DataAnnonations validation attributes as below

    public class Site
    {
        [Required(ErrorMessage = "SiteID can't be null")]
        public int SiteID { get; set; }
        [Required]
        public int House { get; set; }
        [Required]
        public string Street { get; set; }
        [Required]
        [RegularExpression("^[a-zA-Z][a-zA-Z ]*$")]
        public string City { get; set; }
        [Required(ErrorMessage = "State is required")]
        [RegularExpression("^[A-Z][A-Z]$", ErrorMessage = "Incorrect zip code.")]
        public string State { get; set; }
        [Required]
        [RegularExpression("^[0-9][0-9]*$")]
        public string Zip { get; set; }
        public int Apartment { get; set; }
    }
    

    then use this class with ChoCSVReader to load and check the validity of the file using Validate()/IsValid() method as below

    using (var p = new ChoCSVReader("*** YOUR CSV FILE PATH ***")
        .WithFirstLineHeader(true)
        )
    {
        Exception ex;
        Console.WriteLine("IsValid: " + p.IsValid(out ex));
    }
    

    Hope it helps.

    Disclaimer: I'm the author of this library.

提交回复
热议问题