473,385 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

XMLReader file used by another process

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
5 3163
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: xmlguy | last post by:
I believe this should be pretty elementary, but for some reason I cannot seem to think of how to write the an XML file from an incoming XML file. Basically this is what I do: Input: ...
2
by: xmlguy | last post by:
Cant seem to solve this problem I need to be able to re-use XmlReader and XPathDocument for an XSLT Transform. Basically I have defined following interfaces: Class Render (Common and...
1
by: Angus Lepper | last post by:
I'm writing a stock ticker for a stock market simulation, and can load the data into the xmlreader in the first place, but can't figure out how to refresh/update the data in it. Any ideas? Code:...
0
by: Jen | last post by:
My main question: "How can I get a TextReader or Stream object from an existing XmlReader?". Read on for more: I have an existing XmlReader. Let's call it reader1. I'm creating another reader,...
6
by: Rob Meade | last post by:
Hi all, I'm having a few difficulties with the above, ie I cant find any good examples anywhere of how to do what I want to do! I have an xml file on my desktop which I want to read in. ...
6
by: Mitch | last post by:
Hello, I have a singleton class that loads a config file (for an assembly) into an internal stream object. I designed this class as singleton because it parses the config file many times and I...
5
by: bulldog8 | last post by:
I've read numerous posts and have tried multiple approaches that I found, but just cannot get a file renamed. I am using VB.Net 2002 ... Here is what I have tried: 1) Code common to all...
8
by: =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post by:
Hi; I copied a new set of files over to my ASP.NET dir (and subdirs) on our test server. This replaced every file for the app. When I first then tried to bring it up, I got the below error. ...
7
by: Katit | last post by:
I have string with XML, need to read it using XmlReader. I don't see any way to feed string variable in there. How it's done? Thanks!
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.