Reputation: 43
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
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