Hi,
I have a test program that creates 5 threads and each thread uses
XmlDocument.Save(filename) to save a Xml dom to a file. I have put the
lock statement around the Save to prevent from concurrently updating.
However I'm still seeing "System.IO.IOException: The process cannot
access the file "c:\file1.xml" because it is being used by another
process." The following is the test program
public class XmlTest
{
private static void ThreadProc(object o)
{
XmlDocument x1 = new XmlDocument();
x1.AppendChild(x1.CreateElement("test"));
while (running)
{
lock (typeof(XmlTest))
{
try
{
x1.Save(@"c:\file1.xml");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
//...
}
However, if I change
x1.Save(@"c:\file1.xml");
to
using (StreamWriter sw = new StreamWriter(@"c:\file1.xml"))
{
x1.Save(sw);
}
then it seems to work fine. So I think the XmlDocument.Save(filename)
is not closing the file properly. Is this right?
Thanks,
-hengyi