Click here to Skip to main content
15,937,602 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am currently developing a project that saves a setting in an xml file when i test it on my pc it works perfectly as well as to the machine that is used for running the program. my problem is that there are times that when my program would be executed it would says an error, and the error is "Root element is missing." and whenever i check my xml file, the file is already empty. Any idea what is the cause of my problem and any solution for it? Thanks.






VB
Here's my function in editing the nodes of the xml.

Public Function editSettings(ByVal xml_Filename As String, ByVal xml_path As String, ByVal value_name As String, ByVal value1 As String)
Try
'GC.Collect()
Dim xd As New XmlDocument()

xd.Load(xml_Filename)

Dim nod As XmlNode = xd.SelectSingleNode(xml_path & "/" & value_name) '"Configuration/Settings/FirstName"
If nod IsNot Nothing Then
nod.InnerXml = value1
Else
nod.InnerXml = value1
End If

xd.Save(xml_Filename)



Catch ex As Exception

End Try
Posted
Updated 22-Dec-14 22:44pm
v2

1 solution

There is an event that fails to write into the file and you're not handling the error properly.

Find the place where you're writing into the file. Check before writing that you're not writing empty string before opening the file.

Also, wrap it into try...catch...finally and use finally to properly close and dispose of the file handle.

If you're opening the file with rewrite option and then the writing fails, you will get the empty file.
 
Share this answer
 
Comments
Member 11261063 23-Dec-14 4:27am    
I'm already wrapping my codes into try..catch but still the problem occurs, and i also recheck all my codes and i'm sure that i'm not writing empty strings in the file.


Here's my function in editing the nodes of the xml.

Public Function editSettings(ByVal xml_Filename As String, ByVal xml_path As String, ByVal value_name As String, ByVal value1 As String)
Try
'GC.Collect()
Dim xd As New XmlDocument()

xd.Load(xml_Filename)

Dim nod As XmlNode = xd.SelectSingleNode(xml_path & "/" & value_name) '"Configuration/Settings/FirstName"
If nod IsNot Nothing Then
nod.InnerXml = value1
Else
nod.InnerXml = value1
End If

xd.Save(xml_Filename)



Catch ex As Exception

End Try

Any suggestions?
Thanks for the help.
Sinisa Hajnal 23-Dec-14 4:36am    
Please use Improve question link and move this code into the question. You're more likely to get answers. I'll look into this a bit later.

Put MsgBox(ex.Message + vbNewLine + ex.StackTrace) into your catch block (in general, DO NOT EVER make empty exception block). There are few noteable exceptions, but these are really rare.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900