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.Split(delimiter).Select(x => x.Trim('\"')).ToArray();
var xmlOut =
new XElement("Document","",new XAttribute("Pass","true"),
new XElement("DocFile",new XAttribute("FileLoc",@"X:\MyFile.pdf"),
// BEGIN THE PARSING
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]))))))
xmlOut.Save(@output + @"\Output-"+ counter + ".xml");
Any help is appreciated.