Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 11th, 2006, 01:35 AM
liuhengyi@gmail.com
Guest
 
Posts: n/a
Default 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

  #2  
Old April 11th, 2006, 06:46 PM
Sergey Dubinet
Guest
 
Posts: n/a
Default Re: XmlDocument.Save() does not close the file properly?

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

<liuhengyi@gmail.com> wrote in message
news:1142040147.131524.18730@u72g2000cwu.googlegro ups.com...[color=blue]
> 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
>[/color]


 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles