Link to home
Start Free TrialLog in
Avatar of lm1189
lm1189

asked on

Generating XML from a CSV

I'm writing code which iterates through a text file, and releases it in a certain XML format.  My intent is each line generate a separate XML file.  So for example, FileLoc would be the last column in the file, and the only Field Elements you would see, would be the ones on the line.  The number of elements purely dependent on the number of values on the line.

I'm having my most trouble figuring out how can I get my program to iterate on a line by line basis and use the header.  Essentially, it should read the first line (header), use that to name the attribute of the Field Element, push the value in the field and only populate values on a single line, write it to a file, then move to the next line and repeat the process.  

Below is my method and attached is the XML I'm working to match.

        private void processFile(string source, string output, char delimiter)
        {

            int counter = 0;

            var lines = File.ReadAllLines(@source);
            string[] headers = lines[0].Split(delimiter).Select(x => x.Trim('\"')).ToArray();


                var xmlOut =

                new XElement("Import",
                    new XElement("Archive",
                        new XElement("Document","",new XAttribute("Pass","true"), 
                        new XElement("DocFile",new XAttribute("FileLoc",@"X:\MyFile.pdf"),
                        // BEGIN THE PARSING
                        new XElement("Fields",
                           lines.Where((line, index) => index > 0).Select(line =>
                            line.Split(delimiter).Select((column, index) => new XElement("Field", new XAttribute("value", column), new XAttribute("pass", "true"), new XAttribute("confidence", "0"), new XAttribute("Name", headers[index]))))))
                       )));

                counter++;
                xmlOut.Save(@output + @"\Output-"+ counter + ".xml");
           
          }

Open in new window


Any help is appreciated.
TargetXMLFormat.txt
TestFile.txt
ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial