Hoping I might be able to get some help with an infuriating problem which I can't seem to find a definitive answer for anywhere.
I am appending data to an xml document using an XDocument object, and then overwriting the existing file using xDoc.save(path), but for some reason the first time I run the code an error is thrown even though the file is not in use by any other process.
*"The process cannot access the file "C:\XXX\XXXX\Telemetry\2011_11_22.tlm because it is being used by another process."*
Subsequent iterations do not cause a problem.
Here is my code that I am using with try/catch removed for readability:-
XElement x = GenerateTelemetryNode(h); //Create a new element to append
if (File.Exists(path))
{
if (xDoc == null)
{
xDoc = XDocument.Load(new StreamReader(path));
}
}
else
{
xDoc = new XDocument();
xDoc.Add(new XElement("TSD"));
}
xDoc.Element("TSD").Add(x);
xmlPath = path;
xDoc.Save(path);
I'm sure there's a very simple explanation for it.
Many thanks in advance for any replies.
xDoc = XDocument.Load(sr);
– sscheider Jan 18 '14 at 16:48