在VB.NET项目中嵌入和引用XML文件

时间:2015-02-05 15:42:17

标签: vb.net visual-studio-2013 embedded-resource xmldocument

在VS2013 VB.NET WinForms项目中,我需要在已部署的应用程序中包含一个XML文件,该文件将在运行时读取和写入。

我将该文件作为嵌入式资源,并且"始终复制"选择输出。文件名是" Settings.xml"资源名称为“设置”。

查看this示例,我在以下代码中执行了以下操作:

Private xmlFile as XmlDocument ' In the general declaration area, before the Load event
xmlFile.LoadXml(My.Resources.Settings) ' In the Load event, in a Try/Catch

但我得到一个"对象引用未设置为对象的实例"在第二行。

在代码中,我计划使用以下内容访问xml:

Dim xmlDoc as New XmlDocument
xmlDoc = xmlFile

我还不确定如何保存我所做的任何更改,因为xmlDoc.Save(xmlFile)之类的内容的初始尝试无效。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

首先,您需要使用xmlFile的构造函数:

Private xmlFile As New XmlDocument

然后,您需要做的就是使用资源名称,如果资源是嵌入的,则使用“Settings.xml”:

xmlFile.LoadXml("Settings.xml")

您甚至不需要将资源设置为“CopyAlways”。