public Service(Logger logger, FileMgr fileMgr, CsvToXml csvToXml, Validate validate) { this.logger = logger; this.fileMgr = fileMgr; this.csvToXml = csvToXml; this.validate = validate; }
public void RecordsTest() { var columnNames = "'Product','Price','DateStocked'"; var data = "'Pepsi','4.50','2010-05-04'\n'Coke','3.00','2010-09-22'\n'Cheetos','7.25','2009-01-13'"; var csv2xmlFile = TestEnvironment.DataPath + "csv2xml.csv"; File.WriteAllText(csv2xmlFile, columnNames + "\n" + data); var csv2xml = new CsvToXml(csv2xmlFile, true); csv2xml.TextQualifier = '\''; csv2xml.Convert(); Assert.AreEqual(3, csv2xml.Records.Count()); var records = from rec in csv2xml.Records where (decimal)rec.Element("Price") > 3.5m orderby(string) rec.Element("Product") select rec; Assert.AreEqual(2, records.Count()); Assert.AreEqual("Cheetos", (string)records.ElementAt(0).Element("Product")); Assert.AreEqual("Pepsi", (string)records.ElementAt(1).Element("Product")); records = from rec in csv2xml.Records where Convert.ToDateTime((string)rec.Element("DateStocked")) > new DateTime(2010, 9, 1) orderby(string) rec.Element("Product") select rec; Assert.AreEqual(1, records.Count()); Assert.AreEqual("Coke", (string)records.ElementAt(0).Element("Product")); }
public void CsvToXml() { if (ow != null) { var c2x = new CsvToXml(ow, configXml); Assert.IsNotNull(c2x); } }
public void ConstructorTest() { string file = "DOESNT_EXIST.CSV"; var csv = new CsvToXml(file); Assert.AreEqual(file, csv.CsvFile); Assert.AreEqual(',', csv.RecordDelimiter); Assert.AreEqual('"', csv.TextQualifier); Assert.AreEqual(false, csv.HasColumnNames); }
private static void Process() { do { Log("-----------------------------"); Log(" GV = GraphViz"); Log(" CX = Convert .csv to .xml"); Log(" TH = Convert .txt to .html"); Log(" XT = XML table"); Log("-----------------------------"); List <string> commands = new List <string>() { "GV", "CX", "TH", "XT" }; string command = cw.Ask(new QAManager(commands, "GV", "Select a tool")); switch (command.ToUpper()) { case "C": { return; } case "GV": { GraphViz(); break; } case "CX": { csvToXml = new CsvToXml(ow, configXml); csvToXml.Start(); break; } case "TH": { txtToHtml = new TxtToHtml(ow, configXml); txtToHtml.Start(); break; } case "XT": { xDbM = new XmlDBManager(ow, configXml); xDbM.LoadDFD(); xDbM.SaveDFD(); break; } } Log(OK); xm.Save(); } while (true); }
/// <summary> /// Creates an XDocument with a set of documents from the given <paramref name="inputFolderPath"/> and saves it to the <paramref name="outputPath"/>. /// </summary> public static void ConvertDocumentsToXml(string extension, string inputFolderPath, string outputPath) { if (!extension.StartsWith(".")) { extension = $".{extension}"; } XElement rootElement; ToXml converter; switch (extension) { case ".txt": rootElement = new XElement("DocumentSet"); converter = new TxtToXml(); break; case ".csv": rootElement = new XElement("SheetSet"); converter = new CsvToXml(); break; case ".xml": rootElement = new XElement("XmlSet"); converter = new ToXmlViaXslt(_xsltPath); break; default: throw new NotSupportedException($"{extension} is not a supported file format\r\n" + $"Supported formats: {supportedConversionFormats.ShowElements()}."); } string[] files = Directory.GetFiles(inputFolderPath); if (files .Where(file => Path.HasExtension(file)) .All(file => Path.GetExtension(file) == extension)) { var aggregator = new FileAggregator(inputFolderPath); foreach (string file in files) { var doc = new XDocument(); doc.Add(aggregator.ConvertFilesToXml(rootElement, converter)); doc.Save(outputPath); } } else { throw new NotSupportedException($"{inputFolderPath} must only contain files with this extension: {extension} and subfolders"); } }
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"); } } }
public void Convert2Test() { var columnNames = "Product;Price;DateStocked;"; var data = "Pepsi;4.50;2010-05-04;\nCoke;3.00;2010-09-22;\nCheetos;7.25;2009-01-13;"; var csvWithColumnNamesFile = TestEnvironment.DataPath + "csv-columnNames.csv"; var csvFile = TestEnvironment.DataPath + "csv.csv"; File.WriteAllText(csvWithColumnNamesFile, columnNames + "\n" + data); File.WriteAllText(csvFile, data); string xmlHeader = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"; string xmlWithColumnNames = xmlHeader + @"<Records> <Record> <Product>Pepsi</Product> <Price>4.50</Price> <DateStocked>2010-05-04</DateStocked> </Record> <Record> <Product>Coke</Product> <Price>3.00</Price> <DateStocked>2010-09-22</DateStocked> </Record> <Record> <Product>Cheetos</Product> <Price>7.25</Price> <DateStocked>2009-01-13</DateStocked> </Record> </Records>"; var csvWithColumnNames = new CsvToXml(csvWithColumnNamesFile); csvWithColumnNames.RecordDelimiter = ';'; csvWithColumnNames.TextQualifier = null; csvWithColumnNames.HasColumnNames = true; csvWithColumnNames.Convert(); string actualXmlWithColumnNames = csvWithColumnNames.XmlString; Assert.AreEqual(xmlWithColumnNames, actualXmlWithColumnNames); }
static void Main(string[] args) { Logger logger = new Logger(); String fileName = "AppToXML.dll.config"; logger.Initialize(fileName); logger.Info(fileName); FileMgr fileMgr = new FileMgr(); Validate validate = new Validate(); CsvToXml csvToXml = new CsvToXml(); Service service = new Service(logger, fileMgr, csvToXml, validate); service.Init(); service.Process(LevelType.Easy); service.Process(LevelType.Hard); Console.WriteLine("Hello World!"); }
public void ConvertTest() { var columnNames = "'Product','Price','DateStocked'"; var data = "'Pepsi','4.50','2010-05-04'\n'Coke','3.00','2010-09-22'\n'Cheetos','7.25','2009-01-13'"; var csvWithColumnNamesFile = TestEnvironment.DataPath + "csv-columnNames.csv"; var csvFile = TestEnvironment.DataPath + "csv.csv"; File.WriteAllText(csvWithColumnNamesFile, columnNames + "\n" + data); File.WriteAllText(csvFile, data); string xmlHeader = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"; string xmlWithColumnNames = xmlHeader + @"<Records> <Record> <Product>Pepsi</Product> <Price>4.50</Price> <DateStocked>2010-05-04</DateStocked> </Record> <Record> <Product>Coke</Product> <Price>3.00</Price> <DateStocked>2010-09-22</DateStocked> </Record> <Record> <Product>Cheetos</Product> <Price>7.25</Price> <DateStocked>2009-01-13</DateStocked> </Record> </Records>"; var csvWithColumnNames = new CsvToXml(csvWithColumnNamesFile); csvWithColumnNames.RecordDelimiter = ','; csvWithColumnNames.TextQualifier = '\''; csvWithColumnNames.HasColumnNames = true; csvWithColumnNames.Convert(); string actualXmlWithColumnNames = csvWithColumnNames.XmlString; Assert.AreEqual(xmlWithColumnNames, actualXmlWithColumnNames); string xml = xmlHeader + @"<Records> <Record> <Column1>Pepsi</Column1> <Column2>4.50</Column2> <Column3>2010-05-04</Column3> </Record> <Record> <Column1>Coke</Column1> <Column2>3.00</Column2> <Column3>2010-09-22</Column3> </Record> <Record> <Column1>Cheetos</Column1> <Column2>7.25</Column2> <Column3>2009-01-13</Column3> </Record> </Records>"; var csv = new CsvToXml(csvFile); csv.RecordDelimiter = ','; csv.TextQualifier = '\''; csv.HasColumnNames = false; csv.Convert(); var actualXml = csv.XmlString; Assert.AreEqual(xml, actualXml); }