Why can't I save my .xml file in vb.net?

Xavier434

Lifer
Oct 14, 2002
10,373
1
0
What I am trying to do is really basic. All I want is to be able to update the inner text of a couple nodes and then save the XML file. However, whenever I try to save the file, the program crashes and I get this error:

System.IO.IOException "The process cannot access the file 'C:\Documents and Settings\mjason\Desktop\Projects\Global Distribution ? Inventory\Global Distribution ? Inventory\bin\Debug\Settings.xml' because it is being used by another process."


I understand what the error means, but I don't know why it is occurring since nothing else should be using that file. Here is the code:

Private Sub SaveSettings(ByVal Username As String)

Dim xmlDoc As New XmlDocument
Dim xmlRememberNode As XmlNode
Dim xmlUsernameNode As XmlNode
Dim xmlPath As String

' Get the Settings.xml path
xmlPath = Application.StartupPath() + "/Settings.xml"

' Open Settings.xml
xmlDoc.Load(xmlPath)

' Get current xml data for Remember and Username
xmlRememberNode = xmlDoc.SelectSingleNode("//Settings/Remember")
xmlUsernameNode = xmlDoc.SelectSingleNode("//Settings/Username")

' Save the settings
If chkboxRemember.Checked Then
xmlRememberNode.InnerText = "1"
xmlUsernameNode.InnerText = Username
Else
xmlRememberNode.InnerText = "0"
xmlUsernameNode.InnerText = Nothing
End If

xmlDoc.Save(xmlPath)

End Sub


Here is the XML:

<?xml version="1.0" encoding="utf-8" ?>
<Settings>
<Remember>0</Remember>
<Username>Name</Username>
<DatabasePath>Path</DatabasePath>
</Settings>

Do any of you understand why I cannot save the xml file or perhaps see something else that I am doing wrong? I am open for changing this around just to make it work.
 

Tencntraze

Senior member
Aug 7, 2006
570
0
0
You could try creating a FileStream and passing that into the call to xmlDoc.Load instead of the file path. This way you can specifically call Close() on the file stream before you try to save the xmlDoc. If this doesn't work, you may want to get FileMon to see what has a handle on that file.
 

Xavier434

Lifer
Oct 14, 2002
10,373
1
0
That solution in the article makes sense, but I am still getting the same error. The error occurs in the line highlighted in bold. My code now looks like this:

Private Sub SaveSettings(ByVal Username As String)

Dim xmlDoc As XmlDocument
Dim xmlRememberNode As XmlNode
Dim xmlUsernameNode As XmlNode


' Open Settings.xml
xmlDoc = New XmlDocument
Dim fs As New FileStream("Settings.xml", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)
xmlDoc.Load(fs)


' Get current xml data for Remember and Username
xmlRememberNode = xmlDoc.SelectSingleNode("//Settings/Remember")
xmlUsernameNode = xmlDoc.SelectSingleNode("//Settings/Username")

' Update and Save the settings
If chkboxRemember.Checked Then
xmlRememberNode.InnerText = "1"
xmlUsernameNode.InnerText = Username
Else
xmlRememberNode.InnerText = "0"
xmlUsernameNode.InnerText = Nothing
End If

fs.Seek(0, SeekOrigin.Begin)
fs.SetLength(0)
xmlDoc.Save(fs)

End Sub

I also tried FileMon which didn't really help me. I find it strange that this solution would work for some people and not me since my program is currently as simple as it gets. Does anyone else have a suggestion?
 

Tencntraze

Senior member
Aug 7, 2006
570
0
0
Is the error occuring when you try to create the file stream? You could try to open the file with only read access instead of read/write, I suppose, then close that stream and then just save the document using the overload that takes a path.

I copied your function line-for-line and it worked perfectly for me. Do you have the correct permissions to read/write to that location?
 

WannaFly

Platinum Member
Jan 14, 2003
2,811
1
0
It worked for be, but it did not release the file, but I could not get the exception to throw.

If the error is occuring the second time you call this, add fs.close after your save line. Otherwise, are you sure you are not calling it twice or on separate threads? We might need a little more info on your project/solution
 

Xavier434

Lifer
Oct 14, 2002
10,373
1
0
I appreciate everyone's help and I am sure I could have gotten past this issue eventually, but my time crunch forced me to find another way. I am currently using a .xsd file to store the data that I need and it is working just fine. If another portion of this project requires the use of a .xml file then I will try your suggestions. Again, I thank everyone for helping.