I have a program written in .NET 3.5 which saves data to an XML file, and most of the time will leave it alone after that. However, sometimes it needs to read that XML file back in. When I try to do this, the XML file read fails with the error that the file is "in use by another process".
Here's the code that performs the XML file write:
DataSet ds = new DataSet();
ds.Tables.Add(populatedDataTable);
XmlTextWriter xmlWrite = new XmlTextWriter("C:\test\output.xml", Encoding.UTF8);
ds.WriteXml(xmlWrite);
xmlWrite.Close();
And here's the code that tries to read the XML file back in:
TextReader tr = new StreamReader("C:\test\output.xml");
DataSet dsXmlData = new DataSet();
dsXmlData.ReadXml(tr);
Since the XmlTextWriter.Close() method is run in the first block, why would the file still be in use? I tried calling the Dispose() method on the DataSet in the first block as well, but that didn't help. What could be missing here?
I also tested running the program in debug mode, and then waiting a bit after the first code block has run, before continuing. It still gave the same error, this isn't an issue of the code running so close together that the file write isn't fully released before the read is attempted.
Thanks!