问题
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