当机器意外关闭时,继续使用root元素。 [英] keep on getting root element is missing when the machine unexpectedly shuts down.

查看:74
本文介绍了当机器意外关闭时,继续使用root元素。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个项目,当我在我的电脑上测试它时,它在xml文件中保存设置它完美地运行,以及用于运行程序的机器。我的问题是,有时我的程序会被执行,它会说错误,错误是根元素丢失。每当我检查我的xml文件时,该文件已经是空的。知道我的问题的原因是什么,有什么解决方案吗?谢谢。













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.






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

推荐答案

有一个无法写入文件但你没有正确处理错误的事件。



找到你写入文件的地方。在写入之前检查您在打开文件之前没有写入空字符串。



另外,将它包装成try ... catch ...最后并使用finally来正确关闭并处理文件句柄。



如果您使用重写选项打开文件然后写入失败,您将获得空文件。
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.


这篇关于当机器意外关闭时,继续使用root元素。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆