Exemplo n.º 1
0
        protected void AddValueList(string[] fields, string[] columns, int rowIndex)
        {
            dynamic expandoObject     = new ExpandoObject();
            var     validationManager = FileReaderContext.Get <IValidationManager>();
            var     isValid           = true;

            //for loop is used for each column name
            for (var index = 0; index < columns.Length; index++)
            {
                //find column from fields index
                var column = columns[index];
                //find data from fields index
                var data = fields[index];
                //create expando object
                var validationItems = ValidationList.Where(i => i.PropertyName == column).ToArray();
                //if exist a validation for the property
                if (validationItems != null && validationItems.Length > 0)
                {
                    foreach (var validationItem in validationItems)
                    {
                        isValid = validationManager.ValidateData(validationItem.ValidationName, data, validationItem.Parameter);

                        if (!isValid)
                        {
                            var validationError = new ValidationError();
                            validationError.RowNumber         = rowIndex;
                            validationError.ColumnName        = column;
                            validationError.Data              = data;
                            validationError.ValidationMessage = validationItem.ValidationMessage;
                            ErrorList.Add(validationError);
                            expandoObject = null;
                            return;
                        }
                    }
                }
                ((IDictionary <string, object>)expandoObject).Add(column, data);
            }
            ValueList.Add(expandoObject);
            expandoObject = null;
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Reading file starts.");

            FileReaderContext.InitializeContext();
            var fileReaderManager = FileReaderContext.Get <IFileReaderManager>();
            var formatManager     = FileReaderContext.Get <IFormatManager>();

            //we can set validation list which we want validate any area
            var validationList = new List <ValidationItem>();

            validationList.Add(new ValidationItem("name", ValidationNameConstants.UTF8Validation, "Name value must be UTF8 format."));
            validationList.Add(new ValidationItem("uri", ValidationNameConstants.URLValidation, "Uri value must be url format."));
            validationList.Add(new ValidationItem("stars", ValidationNameConstants.IntegerValidation, "Starts value must be integer."));
            validationList.Add(new ValidationItem("name", ValidationNameConstants.MaxLengthValidation, "Name value max length must be 5.", 5));

            Console.WriteLine("Reading CSV file");

            //csv file operations
            var csvFile = new CsvFile("Data\\hotels.csv", validationList);

            fileReaderManager.ReadFile(csvFile);
            var csvValueList = csvFile.GetValueList <ExcelData>();
            var csvErrorList = csvFile.GetErrorList();

            Console.WriteLine("Reading XLS file");

            //excel file operations(xls)
            var xlsExcelFile = new ExcelFile("Data\\hotels.xlsx", validationList, "hotels");

            fileReaderManager.ReadFile(xlsExcelFile);
            var xlsExcelValueList = csvFile.GetValueList <ExcelData>();
            var xlsExcelErrorList = csvFile.GetErrorList();

            Console.WriteLine("Reading XLSX file");

            //excel file operations(xlsx)
            var xlsxExcelFile = new ExcelFile("Data\\hotels.xls", validationList, "hotels");

            fileReaderManager.ReadFile(xlsxExcelFile);
            var xlsxExcelValueList = csvFile.GetValueList <ExcelData>();
            var xlsxExcelErrorList = csvFile.GetErrorList();

            //convert csv file to XML
            Console.WriteLine("Translating data into xml format.");

            var         xmlFormat = formatManager.FormatData(new XMLFormat(), csvFile, "Hotel");
            XmlDocument xdoc      = new XmlDocument();

            xdoc.LoadXml(xmlFormat.Data);
            var xmlName = string.Concat("Data\\hotels.", xmlFormat.Extension);
            var xmlPath = GetPath(xmlName);

            xdoc.Save(xmlPath);

            //convert xls excel file to XML
            Console.WriteLine("Translating data into json format.");

            var jsonFormat = formatManager.FormatData(new JSONFormat(), xlsExcelFile);
            var jsonName   = string.Concat("Data\\hotels.", jsonFormat.Extension);
            var jsonPath   = GetPath(jsonName);

            File.WriteAllText(jsonPath, jsonFormat.Data);


            Console.WriteLine("Process is finished. Check 'Data' folder in project for outputs.");
            Console.ReadKey();
        }