Ejemplo n.º 1
0
        static void SaveFiles(List <Student> students, string fileName)
        {
            // 1. Save CSV file
            string studentCSVPath = $"{Constants.Locations.DataFolder}\\{fileName}.csv";

            //Establish a file stream to collect data from the response. this point is local disk.
            using (StreamWriter fs = new StreamWriter(studentCSVPath))
            {
                int headerNum = 0;
                foreach (var student in students)
                {
                    if (headerNum == 0)
                    {
                        fs.Write(student.HeaderRow + "\r\n");
                        headerNum++;
                    }
                    else
                    {
                        fs.Write(student.ToCSV() + "\r\n");
                    }
                }
            }

            // 2. Save JSON file, which is converted from CSV file
            string studentJSONPath = $"{Constants.Locations.DataFolder}\\{fileName}.json";
            string analyticsData   = CsvToJson.ReadFile(studentCSVPath);

            using (StreamWriter fs = new StreamWriter(studentJSONPath))
            {
                fs.Write(analyticsData);
            }

            // 3. Save XML file, which is converted from CSV file
            string    studentXMLPath = $"{Constants.Locations.DataFolder}\\{fileName}.xml";
            string    csv            = File.ReadAllText(studentCSVPath);
            XDocument doc            = CsvToXml.ConvertCsvToXML(csv, new[] { "," });

            doc.Save(studentXMLPath);

            // 4. Upload the files to My FTP
            foreach (var student in students)
            {
                if (student.MyRecord)
                {
                    FTPApp.UploadFile(studentCSVPath, student.FullPathUrl + "/students.csv");
                    FTPApp.UploadFile(studentJSONPath, student.FullPathUrl + "/students.json");
                    FTPApp.UploadFile(studentXMLPath, student.FullPathUrl + "/students.xml");
                }
            }
        }