public static void GhiFile(List <countries> arr) { string nameFile = "\\23_B2.csv"; string pathFile = Environment.CurrentDirectory + nameFile; try { using (StreamWriter sw = new StreamWriter(pathFile, false, UnicodeEncoding.UTF8)) { sw.WriteLine("country,name,longName,foundingDate,population,capital,largestCity,area"); foreach (var item in arr) { var temp = new countries(); temp = ChuanHoaDuLieu(item); sw.WriteLine(temp.country + "," + temp.name + "," + temp.longName + "," + temp.foundingDate + "," + temp.population + "," + temp.capital + "," + temp.largestCity + "," + temp.area); } Console.WriteLine("Path output : " + pathFile); } } catch (Exception e) { Console.WriteLine(e.Message); } }
private static countries ChuanHoaDuLieu(countries data) { if (data.name.Contains(",")) { data.name = data.name.Replace(",", "-"); } if (data.capital.Contains(",")) { data.capital = data.capital.Replace(",", "-"); } if (data.area.Contains("or")) { data.area = data.area.Replace("or", ""); } return(data); }
public static List <countries> DocFile(string addressFile) { List <countries> listCountries = new List <countries>(); try { using (StreamReader sr = new StreamReader(addressFile)) { string line; int count = 0; int check = 0; bool temp = false; countries tamp = new countries(); while ((line = sr.ReadLine()) != null) { count++; if (count > 8) { check = KiemTraThuocTinh(line); if (check == 1) { if (temp == true) { listCountries.Add(tamp); } tamp = new countries(); temp = true; tamp.country = getText(line); } else { if (check == 2) { tamp.name = getText(line); } if (check == 3) { tamp.longName = getText(line); } if (check == 4) { tamp.foundingDate = getText(line); } if (check == 5) { tamp.population = float.Parse(getText(line)); } if (check == 6) { tamp.capital = getText(line); } if (check == 7) { tamp.largestCity = getText(line); } if (check == 8) { tamp.area = getText(line); } } } } } } catch (Exception e) { Console.WriteLine(e.Message); } return(listCountries); }