?
Solved

Unload or Dispose Xml.XmlDocument.load

Posted on 2004-08-23
8
Medium Priority
?
8,596 Views
Last Modified: 2008-01-09
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?



0
Comment
Question by:carmodyk
  • 4
  • 3
8 Comments
 
LVL 3

Expert Comment

by:DJ_Back-Q
ID: 11870491
Try objDoc.Dispose()

and then objDoc = Nothing.

Andre,
MCSD
0
 
LVL 6

Author Comment

by:carmodyk
ID: 11871623
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.
0
 
LVL 3

Expert Comment

by:DJ_Back-Q
ID: 11871751
Can you post me the code in ReadNodes()

Thanks.
0
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
LVL 6

Author Comment

by:carmodyk
ID: 11872021
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
0
 
LVL 3

Expert Comment

by:DJ_Back-Q
ID: 11872177
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.

0
 
LVL 6

Author Comment

by:carmodyk
ID: 11872476
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".  
0
 
LVL 3

Assisted Solution

by:DJ_Back-Q
DJ_Back-Q earned 1200 total points
ID: 11872531
Well I think it's the only way unless you call the Save method. I think the Save method might release the handle on the document.
0
 
LVL 1

Accepted Solution

by:
johanjohansson earned 800 total points
ID: 11878763
Don't use the XmlDocument to load the file. Use a class that supports the IDispose interface and use the stream instead.

C#

XmlDocument xd = new XmlDocument();

StreamReader sr = new StreamReader( path ) )
xd.Load( sr );
sr.Close();
sr.Dispose();
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question