Solved

Unload or Dispose Xml.XmlDocument.load

Posted on 2004-08-23
8
7,667 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 300 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 200 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Memory Usage 2 50
Authentication of Web Services 3 49
C# HTTP GET method sample code 3 40
How would you add MULTITHREADING to the attached C# code? 4 51
IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

16 Experts available now in Live!

Get 1:1 Help Now