Solved

Unload or Dispose Xml.XmlDocument.load

Posted on 2004-08-23
8
8,129 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

726 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