コード例 #1
0
        /// <summary>
        /// In this method i am using Ienumerable
        /// </summary>
        /// <returns>it returns the string value</returns>
        public int LoadIndiaCSVData(string FilePath, char delimiter = ',')
        {
            FileInfo fileInfo = new FileInfo(FilePath);
            string   type     = fileInfo.Extension;

            if (type != ".CSV" && type != ".csv")
            {
                throw new CensusAnalyseException("INCORRECT_TYPE");
            }

            if (!File.Exists(FilePath))
            {
                throw new CensusAnalyseException("ERROR_IN_FILE_READING");
            }

            string[] lines = File.ReadAllLines(FilePath);

            if (lines.Contains("State,Population,AreaInSqKm,DensityPerSqKm"))
            {
                foreach (var line in lines)
                {
                    ////For delimeter
                    string[] LineCount = line.Split(delimiter);
                    if (LineCount.Length != 4 && LineCount.Length != 2)
                    {
                        throw new CensusAnalyseException("INVALID_DELIMITER");
                    }
                }

                Dictionary <int, StateCensusClass> keyValues = new Dictionary <int, StateCensusClass>();
                int k = 0;
                for (int i = 1; i < lines.Length; i++)
                {
                    StateCensusClass sc = new StateCensusClass
                    {
                        State          = lines[i].Split(',')[0].ToString(),
                        Population     = int.Parse(lines[i].Split(',')[1]),
                        AreaInSqKm     = int.Parse(lines[i].Split(',')[2]),
                        DensityPerSqKm = int.Parse(lines[i].Split(',')[3])
                    };

                    keyValues.Add(k, sc);

                    k++;
                }
                return(lines.Length);
            }
            throw new CensusAnalyseException("HEADER_IS_NOT_FOUND");
        }
コード例 #2
0
        /// <summary>
        /// In this method load csv data for india
        /// </summary>
        /// <returns>it returns the string value</returns>
        public virtual int LoadIndiaCSVData(string FilePath, char delimiter = ',')
        {
            FileInfo fileInfo = new FileInfo(FilePath);
            string   type     = fileInfo.Extension;

            if (type != ".CSV" && type != ".csv")
            {
                throw new CensusAnalyseException("INCORRECT_TYPE");
            }

            if (!File.Exists(FilePath))
            {
                throw new CensusAnalyseException("ERROR_IN_FILE_READING");
            }

            int k = 1;

            using (StreamReader sr = new StreamReader(FilePath))
            {
                string s21 = sr.ReadLine();
                int    del = s21.Split(delimiter).Length;
                if (s21 != "SrNo,StateName,TIN,StateCode" && s21 != "State,Population,AreaInSqKm,DensityPerSqKm")
                {
                    throw new CensusAnalyseException("HEADER_IS_NOT_FOUND");
                }
                else if (del != 4)
                {
                    throw new CensusAnalyseException("INVALID_DELIMITER");
                }
                else if (s21 == "State,Population,AreaInSqKm,DensityPerSqKm")
                {
                    Dictionary <int, StateCensusClass> dict = new Dictionary <int, StateCensusClass>();
                    string s = "";
                    while ((s = sr.ReadLine()) != null)
                    {
                        if (s != "State,Population,AreaInSqKm,DensityPerSqKm")
                        {
                            StateCensusClass sc = new StateCensusClass
                            {
                                State          = s.Split(',')[0].ToString(),
                                Population     = int.Parse(s.Split(',')[1]),
                                AreaInSqKm     = int.Parse(s.Split(',')[2]),
                                DensityPerSqKm = int.Parse(s.Split(',')[3])
                            };

                            dict.Add(k, sc);
                            k++;
                        }
                    }
                    return(dict.Count);
                }

                else if (s21 == "SrNo,StateName,TIN,StateCode")
                {
                    Dictionary <int, StateCodeClass> dict = new Dictionary <int, StateCodeClass>();
                    string s = "";
                    while ((s = sr.ReadLine()) != null)
                    {
                        if (s != "State,Population,AreaInSqKm,DensityPerSqKm")
                        {
                            StateCodeClass sc = new StateCodeClass
                            {
                                SrNo      = int.Parse(s.Split(',')[0]),
                                StateName = s.Split(',')[1].ToString(),
                                TIN       = int.Parse(s.Split(',')[2]),
                                StateCode = s.Split(',')[3].ToString(),
                            };

                            dict.Add(k, sc);
                            k++;
                        }
                    }
                    return(dict.Count);
                }
                return(0);
            }
        }