carmodyk
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.Messag e, 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?
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.Messag
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?
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.
Thanks.
ASKER
Private Sub ReadNodes(ByVal xmlDoc As Xml.XmlDocument)
Dim xAttr As XmlAttribute
mxNodeList = xmlDoc.SelectNodes("//CUST OMER")
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
Dim xAttr As XmlAttribute
mxNodeList = xmlDoc.SelectNodes("//CUST
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.WaitForPendingFinalizer s //Don't forget this line, it waits for everything to be clean up before your code continues.
objDoc = Nothing
GC.Collect
GC.WaitForPendingFinalizer
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
and then objDoc = Nothing.
Andre,
MCSD