grid and xml.write

Hi, I have an xml file that is read into a grid using xml.read. I want to changes made in the grid back to the same file but when I call xml.write with the path/name of that file it gives me an error saying it ''cannot access the file because it is being used in another process''. How can I get around this - and what process is the file being used in after the initial xml.read call? THanks CB

4 Replies

AD Administrator Syncfusion Team February 20, 2004 02:53 PM UTC

I do not think the grid has anything to do with this. Are you closing the reader?


AD Administrator Syncfusion Team February 20, 2004 03:03 PM UTC

Here is the code that opens/reads the xml file If nia_tx Is Nothing Then nia_tx = New DataSet nia_tx.ReadXmlSchema("nia_translation_exchange.xsd") Try ''ac changed to dialog box openFileDialog1.InitialDirectory = "c:\" openFileDialog1.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*" openFileDialog1.FilterIndex = 2 openFileDialog1.RestoreDirectory = True If openFileDialog1.ShowDialog() = DialogResult.OK Then myStream = openFileDialog1.OpenFile() If Not (myStream Is Nothing) Then sFilename = openFileDialog1.FileName nia_tx.ReadXml(openFileDialog1.FileName) myStream.Close() End If End If Catch ex As Exception Dim _x As String = ex.ToString End Try End If And here is the code that attempts to save the file: '' Create the FileStream to write with. Dim myFileStream As New System.IO.FileStream _ (sFilename, System.IO.FileMode.Create) '' Create an XmlTextWriter with the fileStream. Dim myXmlWriter As New System.Xml.XmlTextWriter _ (myFileStream, System.Text.Encoding.Unicode) '' Write to the file with the WriteXml method. nia_tx.WriteXml(myXmlWriter) myXmlWriter.Close() The save works fine when I call a save file dialog box and opt to replace the current file. Is there some way to explicitly close the xml file? Thanks, CB >I do not think the grid has anything to do with this. > >Are you closing the reader?


AD Administrator Syncfusion Team February 20, 2004 03:46 PM UTC

Hi Clay, Disgregard this question - I found the problem - THanks again! >Here is the code that opens/reads the xml file > >If nia_tx Is Nothing Then > nia_tx = New DataSet > nia_tx.ReadXmlSchema("nia_translation_exchange.xsd") > > Try > ''ac changed to dialog box > openFileDialog1.InitialDirectory = "c:\" > openFileDialog1.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*" > openFileDialog1.FilterIndex = 2 > openFileDialog1.RestoreDirectory = True > > If openFileDialog1.ShowDialog() = DialogResult.OK Then > myStream = openFileDialog1.OpenFile() > If Not (myStream Is Nothing) Then > sFilename = openFileDialog1.FileName > nia_tx.ReadXml(openFileDialog1.FileName) > myStream.Close() > End If > End If > > Catch ex As Exception > Dim _x As String = ex.ToString > End Try > End If > >And here is the code that attempts to save the file: > > '' Create the FileStream to write with. > Dim myFileStream As New System.IO.FileStream _ > (sFilename, System.IO.FileMode.Create) > '' Create an XmlTextWriter with the fileStream. > Dim myXmlWriter As New System.Xml.XmlTextWriter _ > (myFileStream, System.Text.Encoding.Unicode) > '' Write to the file with the WriteXml method. > nia_tx.WriteXml(myXmlWriter) > myXmlWriter.Close() > >The save works fine when I call a save file dialog box and opt to replace the current file. Is there some way to explicitly close the xml file? > >Thanks, >CB > >>I do not think the grid has anything to do with this. >> >>Are you closing the reader?


AD Administrator Syncfusion Team February 20, 2004 03:51 PM UTC

Is openFileDialog1 a class member? If so, it is still probably around holding onto the file you want to replace. I would make it a local variable and see if it going out of scope will release the file.

Loader.
Up arrow icon