0

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.

1
  • 1
    A tip: You can use the Localized Error Message Lookup to translate the error message from your language to english. Commented Apr 3, 2017 at 8:54

1 Answer 1

3

You are trying to append your Item elements directly to the XML document and XML documents are not allowed to have more than one root element. That is why you are getting an error.

I presume you actually want to add them to the Inventory element, in which case you should do this:

public void LoadFile() {
    var outputDocument = new XDocument();
    var inventory = new XElement("Inventory");
    outputDocument.Add(inventory);

    using (var fstream = File.OpenRead(FilePathProducts))
    using (var fileReader = new StreamReader(fstream)) {
        while (!fileReader.EndOfStream) {
            var readLine = fileReader.ReadLine();
            var words = readLine.Split(';');


            inventory.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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

That's exactly what I wanted. =) Although.. Now the last line is giving me problem outputDocument.Save(FilePathResult); It says something about "resulting in an invalid xml-document"?
@Phrosen What is the exact error? Sounds like you're using .NET in a non-English language so feel free to post it in its original language.
Sorry. I just realized I had missed a line of code. It works now. Thank you. =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.