public static Records CSVParser(string[] aFile) { //Declare variable Records aRecordList = new Records(); //loop through each line from CSV File foreach (string line in aFile) { line.Trim(); //Parse through each attribute using (StringReader stream = new StringReader(line)) //Use TextField Parser in the event delimator include quotes using (TextFieldParser parser = new TextFieldParser(stream)) { parser.SetDelimiters(new string[] { "," }); parser.HasFieldsEnclosedInQuotes = true; while (!parser.EndOfData) { Record rR = new Record(); string[] fields = parser.ReadFields(); Debug.WriteLine(fields.Length); foreach (var field in fields) { Debug.WriteLine(field); } //Assign csv row to Object rR.FIRSTNM = fields[0]; rR.LASTNM = fields[1]; rR.PRNUM = fields[2]; rR.AMOUNT = fields[3]; rR.DATE = Convert.ToDateTime(fields[4]); rR.DEPT = fields[5]; //Add Object to List<> aRecordList.Add(rR); } } } return(aRecordList); }
static void Main(string[] args) { //Directory is a Folder named "CSVs" on DesKtop string dir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\CSVs"; //Multiply Files? //string [] array1=Directory.GetFiles() //Single File Path string path = dir + "\\csvTest.csv"; //File Name for Conversion StringBuilder newFile = new StringBuilder(); // holder to build file Records RecordList = new Records(); string[] file = new string[] { }; //Read CSV file into arrary file = File.ReadAllLines(path).Skip(1).ToArray(); //excute the CsvParser - csv convert to Record Object -- to Record List RecordList = CSVeXtention.CSVParser(file); //invoke class XMLeXtension aExt = new XMLeXtension(); //Pass list into XmlParser -- object convert to xml aExt.XMLParser(RecordList); }
public void XMLParser(Records aRecords) { //Folder Directory for Writing string dir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\CSVs\\XMLConv"; StringBuilder aFileName = new StringBuilder();; //Creating one document for all records XElement xmlElements = new XElement("Records"); try { //var xEle = new XElement("Records"); //Each Record foreach (Record aRecord in aRecords) { aFileName.Clear(); aFileName.AppendFormat("AFS{0}PR", aRecord.PRNUM); XDocument PRRecord = new XDocument( //var iElement = new XElement("PRRECORD", new XElement("PRRECORD", new XElement("FirstName", aRecord.FIRSTNM), new XElement("LastName", aRecord.LASTNM), new XElement("PR_Number", aRecord.PRNUM), new XElement("Amount", aRecord.AMOUNT), new XElement("Date", aRecord.DATE), new XElement("Department", aRecord.DEPT) )); //TODO Write each record as a seperate file //TODO Retreive each file and assign xslt //Create transform object and Load XSLT using (Stream strm = Assembly.GetExecutingAssembly().GetManifestResourceStream("XMLImage.XSLTPR.xslt")) using (XmlReader reader = XmlReader.Create(strm)) using (XmlWriter writer = PRRecord.CreateWriter()) { XslCompiledTransform aXslTrans = new XslCompiledTransform(); aXslTrans.Load(reader); aXslTrans.Transform(PRRecord.CreateNavigator(), writer); Console.WriteLine(PRRecord); PRRecord.Save(dir + "\\" + aFileName + ".xml"); File.WriteAllText(dir + "\\" + aFileName + ".txt", PRRecord.ToString()); //TODO Transfer PRROCRDS.DOCUMENT to PDF //SAVE document Console.WriteLine("You made here Yah!"); PRRecord.Document.RemoveNodes(); //aXslTrans. } //PRRecord.Save(dir + "\\" + aFileName +".xml"); //aXslTrans //TODO After transform Create PDF or Image //TO Save each PDF or Image //xEle.Add(iElement); } //aXslTrans.Transform(xEle, null); //xEle.Save(dir + "\\PRRecord2.xml"); //File.WriteAllText(dir + "\\PRRecords2.txt", xEle.ToString()); Console.WriteLine("Converted to XML"); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadLine(); }