Solved

Why a xml file remain being used by a process after loaded with MyDoc.Load method ??

Posted on 2008-06-24
4
238 Views
Last Modified: 2010-04-21
Hi

I have a VB NET application.

In the main form, I am creating an xml file, and after that, I am calling a child form that tries to read it xml file, and proccess it.

The first time that I run it process, all is OK, in other words, the xml file is successfully created, after that, the other form is called, and them, the xml file is successfully acceded.
.
The next times, the xml file is created ok with new data, but when I call the child form, I can not open the xml file, and I receive the following error:

The process cannot access the file 'C:\Documents and Settings\LUIS\Local Settings\MyCompany\Cache\MyFile.xml' because it is being used by another process.

The only way I can release it, is to exit of my application.

What I am doing wrong?

Why the xml file remain being used by a process ?

Please see the attached example.

Thanks in advance

Luis
' This is the sub in the main form
 
Dim MyDoc As New XmlDocument()
Dim NewElement As XmlElement = Nothing
Dim NewAtribute As XmlAttribute = Nothing
 
MyDoc.LoadXml("<XmlData></XmlData>") ' I allway will create the file
 
NewElement = MyDoc.CreateElement("Headers")
 
NewAtribute = MyDoc.CreateAttribute("Value1")
NewAtribute.Value = "1"
NewElement.Attributes.Append(NewAtribute)
 
NewAtribute = MyDoc.CreateAttribute("Value2")
NewAtribute.Value = "2"
NewElement.Attributes.Append(NewAtribute)
 
MyDoc.DocumentElement.AppendChild(NewElement)
 
...
... More rows are inserted as needed
...
 
 
Dim UserPath As String = Path.GetFullPath(Path.Combine (Environment.GetFolderPath   (Environment.SpecialFolder.LocalApplicationData), "..\\MyCompany\Cache"))
 
Dim Writer As XmlTextWriter = New XmlTextWriter(UserPath & "\MyFile.xml", Nothing)
 
Writer.Formatting = Formatting.Indented
 
' This allways write OK, the first time, and the next times....
MyDoc.Save(Writer) 
 
MyDoc = Nothing
 
ChildForm.ShowDialog() ' Here I call the child form
 
 
********************************************************************
 
 
Following the child form:
 
 
Private Sub ChildForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
Dim ListNodes_Headers As XmlNodeList = Nothing
Dim Node As XmlNode = Nothing
 
Dim MyDoc As New XmlDocument()
 
Dim UserPath As String = Path.GetFullPath(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "..\\MyCompany\Cache"))
 
' The following, fails after the first time
MyDoc.Load(UserPath & "\MyFile.xml") ' Here I receive the error
 
ListNodes_Headers = MyDoc.SelectNodes("//Headers")
 
For Each Node In ListNodes_Headers
 
    Dim Value1 as string = Node.Attributes("Value1").Value
    Dim Value2 as string = Node.Attributes("Value2").Value
 
Next
 
MyDoc = Nothing
 
End Sub

Open in new window

0
Comment
Question by:luiggye
[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
  • 2
4 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 21862337
Hi luiggye;

You need to release the file resources after using it. see sample code.

MyDoc.Save(Writer)
' Close the file
Writer.Close()
MyDoc = Nothing

Fernando
0
 
LVL 14

Expert Comment

by:rachitkohli
ID: 21863961
Try using with stream


        Dim fs As IO.FileStream
        fs = New IO.FileStream("FILE_PATH", IO.FileMode.Open, IO.FileAccess.Read)
        MyDoc .Load(fs)
        fs.Close()
0
 

Author Closing Comment

by:luiggye
ID: 31470408
Thanks Fernando, It worked...
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 21865101
Not a problem, glad I was able to help. ;=)
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 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