469,963 Members | 2,022 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

XmlDocument.Save() does not close the file properly?

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

Mar 11 '06 #1
1 7116
What version of Framework do you use?
On .NET 2.0 I can't repro this behavior.
It is possible that this was a bug in Framework 1.1 and it is fixed already.
Sergey

<li*******@gmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
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

Apr 11 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Casper JH Erasmus | last post: by
4 posts views Thread by Roshawn Dawson | last post: by
reply views Thread by liuhengyi | last post: by
3 posts views Thread by Stephen Ward | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.