Link to home
Start Free TrialLog in
Avatar of carmodyk
carmodykFlag for United States of America

asked on

Unload or Dispose Xml.XmlDocument.load

Help experts!  I'm having an issue where I have a service monitoring a file folder that when a new XML document is put in, it will read the file and capture the nodes information.  Capturing works and everything is fine, except when it wants to do it twice.  First file it finds, no problem.  Second time through I get this error:

An unhandled exception of type 'System.IO.IOException' occurred in system.xml.dll
Additional information: The process cannot access the file "(file Name)" because it is being used by another process.

The only way it works is if I put in the gc.collect statement at the end of the Sub routine. Here is my code:

Dim objDoc As New Xml.XmlDocument
        Dim StrExtension As String

        objDoc.Load(FullPath)
     
        Try
                 ReadNodes(objDoc)
           
        Catch exp As XmlException
            MoveErrorFile(FullPath)
     
        Catch exp As Exception
             MessageBox.Show(exp.Message, exp.Source, MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try
        GC.Collect()

I'm not sure if GC.Collect is the best way to handle this.  I'd rather find a way to unload the objdoc and clear it out of memory without implementing the gc.collect.  Any suggestions?



Avatar of DJ_Back-Q
DJ_Back-Q

Try objDoc.Dispose()

and then objDoc = Nothing.

Andre,
MCSD
Avatar of carmodyk

ASKER

It states that "Dispose is not a member of xml.XmlDocument" and having objDoc = nothing still produces the same error.  If you have a snipit of code using the objdoc.Dispose(), I'd love to see it.
Can you post me the code in ReadNodes()

Thanks.
Private Sub ReadNodes(ByVal xmlDoc As Xml.XmlDocument)

       Dim xAttr As XmlAttribute

        mxNodeList = xmlDoc.SelectNodes("//CUSTOMER")

        If Not (mxNodeList Is Nothing) Then

            For Each mxNode In mxNodeList
                For Each xAttr In mxNode.Attributes

                    Select Case xAttr.Name

                        Case "ID"
                            txtID.Text = Trim(xAttr.Value)
                        Case "NAME"
                            txtNAME.Text = Trim(xAttr.Value)
                        Case "CITY"
                            txtCITY.text = Trim(xAttr.Value)
                        Case "STATE"
                            txtState.text = Trim(xAttr.Value)
                   End Select
                Next
            Next

       End If
    End Sub
Well you solve it with this code.


objDoc = Nothing
GC.Collect
GC.WaitForPendingFinalizers //Don't forget this line, it waits for everything to be clean up before your code continues.

I didn't want to use the GC.Collect, unless there is no alternative.  I heard it's better to dispose your objects then to use the GC.Collect to do "the dirty work".  
SOLUTION
Avatar of DJ_Back-Q
DJ_Back-Q

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial