在VB.NET项目中嵌入和引用XML文件 [英] Embedding and referencing XML file in VB.NET project

查看:262
本文介绍了在VB.NET项目中嵌入和引用XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In a VS2013 VB.NET WinForms project I need to include an XML file in the deployed application that will be read from and written to at run time.

I将文件作为嵌入式资源,并选择始终复制作为输出。文件名为 Settings.xml,资源名称为设置。

I have the file as an embedded resource, and have "Copy Always" selected for output. The file name is "Settings.xml" and the resource name is Settings.

查看

Looking at this example I did the following to reference it in my code:

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

但是我在第二行收到对象引用未设置为对象的实例。

But I get an "Object reference not set to an instance of an object" on the second line.

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

In the code I plan on accessing the xml with something like this:

Dim xmlDoc as New XmlDocument
xmlDoc = xmlFile

我不确定如何保存任何更改使得,例如 xmlDoc.Save(xmlFile)之类的尝试不起作用。

I'm not sure yet how to save any changes I make, as initial attempts of something like xmlDoc.Save(xmlFile) didn't work.

我是什么

推荐答案

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

First of all, you need to use the constructor for xmlFile:

Private xmlFile As New XmlDocument

然后,您需要做的就是使用资源名称,即 Settings.xml嵌入资源:

Then, all you need to do is use the resource name, i.e. "Settings.xml" if the resource is embedded:

xmlFile.LoadXml("Settings.xml")

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

You shouldn't even need to set the resource as "CopyAlways".

这篇关于在VB.NET项目中嵌入和引用XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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