xmlreader question

  • Thread starter Thread starter AMP
  • Start date Start date
A

AMP

Hello,
I have:
writer = XmlWriter.Create("mike.xml",settings);
// ......Do Some stuff. This works writes an xml file.
// Then I try to close it
writer.Flush();
writer.Close();
//But I get an error saying I cant open it, its being used
FileStream fs = new FileStream("mike.xml", FileMode.Open);
....More stuff

What am I missing?
Thanks
 
Hello,
I have:
writer = XmlWriter.Create("mike.xml",settings);
// ......Do Some stuff. This works writes an xml file.
// Then I try to close it
writer.Flush();
writer.Close();
//But I get an error saying I cant open it, its being used
FileStream fs = new FileStream("mike.xml", FileMode.Open);
....More stuff

What am I missing?
Thanks

Try:

using (XmlWriter writer = XmlWriter.Create("mike.xml", settings))
{
// do stuff...
writer.Flush();
writer.Close();
}

using (FileStream fs = new FileStream("mike.xml", FileMode.Open))
{
}
 
AMP said:
Hello,
I have:
writer = XmlWriter.Create("mike.xml",settings);
// ......Do Some stuff. This works writes an xml file.
// Then I try to close it
writer.Flush();
writer.Close();
//But I get an error saying I cant open it, its being used
FileStream fs = new FileStream("mike.xml", FileMode.Open);
....More stuff

What am I missing?

As Mark says, the code you posted as-is works fine. So obviously the
problem is related to code you didn't post.

You should post a concise-but-complete code example that reliably
demonstrates the problem. Only with that can any of us confidently say
exactly what the problem is.

Given your stated error, most likely you have failed to properly close
the original file somehow. But there's no way to know where that
particular problem is if you don't show the relevant code.

Pete
 
I have:
writer = XmlWriter.Create("mike.xml",settings);
// ......Do Some stuff. This works writes an xml file.
// Then I try to close it
writer.Flush();
writer.Close();
//But I get an error saying I cant open it, its being used
FileStream fs = new FileStream("mike.xml", FileMode.Open);
....More stuff

What am I missing?

Are you sure that Close actually gets called?

Is the same file being opened by some other code within your app?

Arne
 
Try:

using (XmlWriter writer = XmlWriter.Create("mike.xml", settings))
{
// do stuff...
writer.Flush();
writer.Close();
}

using (FileStream fs = new FileStream("mike.xml", FileMode.Open))
{
}

The important point being that using will ensure that Dispose
actually gets called.

There should not be functional differences between Close and
Close+Dispose.

Arne
 
Back
Top