Generating XML from a CSV

lm1189
lm1189 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Engineer
Commented:
I'm no LINQ expert myself (so might not be the cleanest solution) but try this:
        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();

            lines.Skip(1).All(line =>
            {

                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",

                            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");
                return true;
            }
            );

        }

Open in new window

You would need to add some more elements and attributes to get to your example output but it seemed to me that's not the hardest part.

This is what I used as input file:
Vendor Name;Invoice Number;Invoice Date;Invoice Amount;Due Date;Transaction Number;Expiration Date
ABC;123;2011-01-01T05:00:00Z;100;2011-01-01T05:00:00Z;164598;2011-01-01T05:00:00Z
DEF;456;2012-01-01T05:00:00Z;111;2012-01-01T05:00:00Z;164599;2012-01-01T05:00:00Z

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial