Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating/Writing to xml file

Status
Not open for further replies.

motte

Programmer
May 31, 2001
155
US
Hi everyone - I have a problem when I first try to create an xml file and write to it. If the file doesn't exist, I create the xml file, then I write to it using a dataset. The problem is, if the file doesn't exist, I create the file then when I try to write to it, an exception is thrown saying the File is in use by another process. Any subsequent tries, after the first try, work perfectly. I can't figure out what is happening. Can anyone help? Here is the code block that fails:

Code:
' strFullPath is the path to the xml file
fi = new FileInfo(strFullPath)

if fi.Exists AND fi.Extension.Equals(".xml") then
  trace.write("File exists and is of type xml")
else
  fi.Create()
  trace.write("Creating file")
end if
		
  trace.write("Connecting to db")
  createSQLConnection 'call to create a connection

  sqlDA = new SqlDataAdapter(strSQLStatement, myConn)

  sqlDA.Fill(dsDBData, "reservation")
	
  ' both lines below don't work...this is where it goes bad
  ' dsDBData.WriteXml(strFullPath,XmlWriteMode.IgnoreSchema)
    dsDBData.WriteXml(strFullPath)

I'd really like an answer to what is wrong and a possible solution. But if there is an alternative way of doing this, I'd like to know that as well. But learning the problem is best.

Thanks,
Mike
 
try setting that fi object = nothing....

that's the only thing I could think of is that somehow, that thing still has a handle on it after creation
penny1.gif
penny1.gif
 
I should've posted that I got an answer from somewhere else. The fileInfo objects Create() method returns a FileStream object. Closing that object solves the problem.

Mike
 
We ran into a similar problem. Whats happening is that when you save a file, you basically hand that off to the operating system and say "here you go, save it!"

However, the OS might take its time (maybe it has other processes to go through first), and your code that tries to write to that file gets to the directory before the OS actually saves it.

What we did for a workaround was this:

While Not File.Exists(strXMLFile)
System.Threading.Thread.Sleep(5000)
End While

This checks if the file exists. If it doesn't, it halts the thread for 5000 miliseconds, then checks again. Once the file does exist, it exits the loops and continues working with the file.

hth

D'Arcy
 
jfrost - that's an interesting way of handling it. I believe your reason why my code was failing is correct. I had the same feeling. I'm going to take a look into the threading in .net. Did some multithreading in java before...would like to check on it in vb.net/c#

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top