I am trying to create an XML file based on a .csv file. I figured I could use my old method for reading the .csv file and just replace some parts. However I get an exception saying:
InvalidOperationException
Here is my code:
public void LoadFile() {
XDocument outputDocument = new XDocument();
outputDocument.Add(new XElement("Inventory"));
using (var fstream = File.OpenRead(FilePathProducts))
using (var fileReader = new StreamReader(fstream)) {
while (!fileReader.EndOfStream) {
var readLine = fileReader.ReadLine();
var words = readLine.Split(';');
outputDocument.Add(
new XElement("Item",
new XElement("Name", words[1]),
new XElement("Count", words[3]),
new XElement("Price", words[4]),
new XElement("Comment", "<Missing Value>"),
new XElement("Artist", "<Missing Value>"),
new XElement("Publisher", "Nintendo"),
new XElement("Genre", "<Missing Value>"),
new XElement("Year", words[0]),
new XElement("ProductID", words[2])
)
);
}
}
outputDocument.Save(FilePathResult);
}
It says something about "a wrongfully structured document is created" (roughly translated.)
When I tried removing the loop, the output file only displayed a self-closing Inventory-tag. I expected it to be an open and close -tag. So I'm guessing something is wrong with line 3 of the code above.