Solved

Unload or Dispose Xml.XmlDocument.load

Posted on 2004-08-23
8
7,548 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
Comment Utility
Try objDoc.Dispose()

and then objDoc = Nothing.

Andre,
MCSD
0
 
LVL 6

Author Comment

by:carmodyk
Comment Utility
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
Comment Utility
Can you post me the code in ReadNodes()

Thanks.
0
 
LVL 6

Author Comment

by:carmodyk
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:DJ_Back-Q
Comment Utility
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
Comment Utility
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 300 total points
Comment Utility
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 200 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now