Embedding and referencing XML file in VB.NET project

本小妞迷上赌 提交于 2019-12-25 16:25:34

问题


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 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.

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

Dim xmlDoc as New XmlDocument
xmlDoc = 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.

What am I missing?


回答1:


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

Private xmlFile As New XmlDocument

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

xmlFile.LoadXml("Settings.xml")

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



来源:https://stackoverflow.com/questions/28348105/embedding-and-referencing-xml-file-in-vb-net-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!