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
lm1189Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Robert SchuttSoftware EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.