OreOS
OreOS

Reputation: 43

Token EndDocument in state Document would result in an invalid XML document when using .Save()

I'm writing an XML file using C# and Linq but when trying to save it gives me the 'Token EndDocument in state Document would result in an invalid XML document' error. The code for the creation and saving of the document:

XDocument xDoc = new XDocument();
using (var db = new CarRentalEntities1())
{
    foreach (Car c in db.Cars)
    {
        XElement root = new XElement("root",
        new XElement
            (
                "Car-" + c.CarName,
                new XAttribute("CarID", c.CarID),
                new XAttribute("CarName", c.CarName),
                new XAttribute("CarType", c.CarType),
                new XAttribute("Reg", c.Registration),
                new XAttribute("YearOfPurchase", c.YearOfPurchase)
            )
        );
    }
    xDoc.Save("D:\\Cars.XML");
}

Upvotes: 3

Views: 4613

Answers (1)

Giles Roberts
Giles Roberts

Reputation: 6883

You're not adding anything to xDoc, so you're saving an empty XDocument to the file.

From @MarcinJuraszek comment above.

Upvotes: 8

Related Questions