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");
}