By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,196 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,196 IT Pros & Developers. It's quick & easy.

XMLReader file used by another process

P: n/a
I've got this application that watches a directory for XML's and then
parses them into a DB. It then moves them based on if the XML was
succesful or a bad XML...

foreach(FileInfo file in dir.GetFiles("*.xml"))
{
try
{
ParseXML(file.FullName);
}
catch
{
FilesToMoveError.Add(file.FullName);
break;
}
FilesToMove.Add(file.FullName);
fileCount++;
}
}

moveFiles();

The parsing is as such, and this is my latest iteration to attempt to
make sure hte reader is closed.

public void ParseXML(string filename)
{
XmlReader xmlReader = null;
try
{
FileStream stream = new FileStream(filename,
FileMode.Open,FileAccess.Read);
xmlReader = new XmlTextReader (stream);

while(!xmlReader.EOF)
{
xmlReader.Read();
switch (xmlReader.NodeType)
{
}
}
}
catch (XmlException e)
{
DebugLog(null, "Error parsing XML " + e.Message);
}
finally
{
if (xmlReader != null)
{
xmlReader.Close();
}
}
}

When I hit the file.move in my move files function it gives me the
exception "file used by another process"

What am I missing??
Thanks
Nov 30 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Maybe you need to close the FileStream too?

--Peter
"Inside every large program, there is a small program trying to get out."
http://www.eggheadcafe.com
http://petesbloggerama.blogspot.com
http://www.blogmetafinder.com

"he*****@gmail.com" wrote:
I've got this application that watches a directory for XML's and then
parses them into a DB. It then moves them based on if the XML was
succesful or a bad XML...

foreach(FileInfo file in dir.GetFiles("*.xml"))
{
try
{
ParseXML(file.FullName);
}
catch
{
FilesToMoveError.Add(file.FullName);
break;
}
FilesToMove.Add(file.FullName);
fileCount++;
}
}

moveFiles();

The parsing is as such, and this is my latest iteration to attempt to
make sure hte reader is closed.

public void ParseXML(string filename)
{
XmlReader xmlReader = null;
try
{
FileStream stream = new FileStream(filename,
FileMode.Open,FileAccess.Read);
xmlReader = new XmlTextReader (stream);

while(!xmlReader.EOF)
{
xmlReader.Read();
switch (xmlReader.NodeType)
{
}
}
}
catch (XmlException e)
{
DebugLog(null, "Error parsing XML " + e.Message);
}
finally
{
if (xmlReader != null)
{
xmlReader.Close();
}
}
}

When I hit the file.move in my move files function it gives me the
exception "file used by another process"

What am I missing??
Thanks
Nov 30 '07 #2

P: n/a
I just added the filestream stuff, it was hanging before that, but
I'll give it a shot.

Thanks for the reply.

On Nov 30, 12:25 pm, Peter Bromberg [C# MVP]
<pbromb...@yahoo.NoSpamMaam.comwrote:
Maybe you need to close the FileStream too?

--Peter
"Inside every large program, there is a small program trying to get out."http://www.eggheadcafe.comhttp://petesbloggerama.blogspot.comhttp://www.blogmetafinder.com

"heda...@gmail.com" wrote:
I've got this application that watches a directory for XML's and then
parses them into a DB. It then moves them based on if the XML was
succesful or a bad XML...
foreach(FileInfo file in dir.GetFiles("*.xml"))
{
try
{
ParseXML(file.FullName);
}
catch
{
FilesToMoveError.Add(file.FullName);
break;
}
FilesToMove.Add(file.FullName);
fileCount++;
}
}
moveFiles();
The parsing is as such, and this is my latest iteration to attempt to
make sure hte reader is closed.
public void ParseXML(string filename)
{
XmlReader xmlReader = null;
try
{
FileStream stream = new FileStream(filename,
FileMode.Open,FileAccess.Read);
xmlReader = new XmlTextReader (stream);
while(!xmlReader.EOF)
{
xmlReader.Read();
switch (xmlReader.NodeType)
{
}
}
}
catch (XmlException e)
{
DebugLog(null, "Error parsing XML " + e.Message);
}
finally
{
if (xmlReader != null)
{
xmlReader.Close();
}
}
}
When I hit the file.move in my move files function it gives me the
exception "file used by another process"
What am I missing??
Thanks
Nov 30 '07 #3

P: n/a
Didn't help.

added the file stream declaration near the reader and then
if (xmlReader != null)
{
xmlReader.Close();
stream.close();
}
Still got the error.

On Nov 30, 1:15 pm, Harry <heda...@gmail.comwrote:
I just added the filestream stuff, it was hanging before that, but
I'll give it a shot.

Thanks for the reply.

On Nov 30, 12:25 pm, Peter Bromberg [C# MVP]

<pbromb...@yahoo.NoSpamMaam.comwrote:
Maybe you need to close the FileStream too?
--Peter
"Inside every large program, there is a small program trying to get out."http://www.eggheadcafe.comhttp://petesbloggerama.blogspot.comhttp://w...
"heda...@gmail.com" wrote:
I've got this application that watches a directory for XML's and then
parses them into a DB. It then moves them based on if the XML was
succesful or a bad XML...
foreach(FileInfo file in dir.GetFiles("*.xml"))
{
try
{
ParseXML(file.FullName);
}
catch
{
FilesToMoveError.Add(file.FullName);
break;
}
FilesToMove.Add(file.FullName);
fileCount++;
}
}
moveFiles();
The parsing is as such, and this is my latest iteration to attempt to
make sure hte reader is closed.
public void ParseXML(string filename)
{
XmlReader xmlReader = null;
try
{
FileStream stream = new FileStream(filename,
FileMode.Open,FileAccess.Read);
xmlReader = new XmlTextReader (stream);
while(!xmlReader.EOF)
{
xmlReader.Read();
switch (xmlReader.NodeType)
{
}
}
}
catch (XmlException e)
{
DebugLog(null, "Error parsing XML " + e.Message);
}
finally
{
if (xmlReader != null)
{
xmlReader.Close();
}
}
}
When I hit the file.move in my move files function it gives me the
exception "file used by another process"
What am I missing??
Thanks
Nov 30 '07 #4

P: n/a
Be carefull that a file appear in the directory as soon as it is *being*
created. That means that the file creator may not have closed it when you
are trying to move it.
One way to avoid this might be to ask the creator to create/copy the file
with another extension and rename it when the file is ready for use.

/LM

<he*****@gmail.comwrote in message
news:f4**********************************@a35g2000 prf.googlegroups.com...
I've got this application that watches a directory for XML's and then
parses them into a DB. It then moves them based on if the XML was
succesful or a bad XML...

foreach(FileInfo file in dir.GetFiles("*.xml"))
{
try
{
ParseXML(file.FullName);
}
catch
{
FilesToMoveError.Add(file.FullName);
break;
}
FilesToMove.Add(file.FullName);
fileCount++;
}
}

moveFiles();

The parsing is as such, and this is my latest iteration to attempt to
make sure hte reader is closed.

public void ParseXML(string filename)
{
XmlReader xmlReader = null;
try
{
FileStream stream = new FileStream(filename,
FileMode.Open,FileAccess.Read);
xmlReader = new XmlTextReader (stream);

while(!xmlReader.EOF)
{
xmlReader.Read();
switch (xmlReader.NodeType)
{
}
}
}
catch (XmlException e)
{
DebugLog(null, "Error parsing XML " + e.Message);
}
finally
{
if (xmlReader != null)
{
xmlReader.Close();
}
}
}

When I hit the file.move in my move files function it gives me the
exception "file used by another process"

What am I missing??
Thanks

Dec 1 '07 #5

P: n/a
That's very true Luc, and it's what we do on several other options,
but this process is supposed to be kicked off after the files are
moved into the directory, in the instance that I'm testing, the file
has been in the directory for a while, and when I try to move it using
the code snippet, it throws the "in use" error on the move command.

Luc E. Mistiaen wrote:
Be carefull that a file appear in the directory as soon as it is *being*
created. That means that the file creator may not have closed it when you
are trying to move it.
One way to avoid this might be to ask the creator to create/copy the file
with another extension and rename it when the file is ready for use.

/LM

<he*****@gmail.comwrote in message
news:f4**********************************@a35g2000 prf.googlegroups.com...
I've got this application that watches a directory for XML's and then
parses them into a DB. It then moves them based on if the XML was
succesful or a bad XML...

foreach(FileInfo file in dir.GetFiles("*.xml"))
{
try
{
ParseXML(file.FullName);
}
catch
{
FilesToMoveError.Add(file.FullName);
break;
}
FilesToMove.Add(file.FullName);
fileCount++;
}
}

moveFiles();

The parsing is as such, and this is my latest iteration to attempt to
make sure hte reader is closed.

public void ParseXML(string filename)
{
XmlReader xmlReader = null;
try
{
FileStream stream = new FileStream(filename,
FileMode.Open,FileAccess.Read);
xmlReader = new XmlTextReader (stream);

while(!xmlReader.EOF)
{
xmlReader.Read();
switch (xmlReader.NodeType)
{
}
}
}
catch (XmlException e)
{
DebugLog(null, "Error parsing XML " + e.Message);
}
finally
{
if (xmlReader != null)
{
xmlReader.Close();
}
}
}

When I hit the file.move in my move files function it gives me the
exception "file used by another process"

What am I missing??
Thanks
Dec 3 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.