0

In a VB.NET project I have an xml document as an embedded resource. I am accessing it with

Private xmlFile as New XmlDocument()

in the General Declaration area. And then I am loading it in the form load method:

xmlFile.LoadXml(My.Resources.Settings)

In a method I'm finding specific nodes and updating them from user input:

'Dim xmlDoc as XmlDocument
'xmlDoc = xmlFile

Dim settingNodes As XmlNodeList = xmlFile.SelectNodes("//Program/ProgramTitle")

For Each setting As XmlNode In settingNodes
    If setting.InnerText = title Then
        setting.ParentNode.Item("ProgramSaveFolder").InnerText = programFolder
        setting.ParentNode.Item("PrimaryBackupFolder").InnerText = primBackup
        setting.ParentNode.Item("SecondaryBackupFolder").InnerText = secBackup
    End If
Next

' Neither of these work
xmlFile.Save("Settings.txt")
'xmlDoc.Save("GameSettings.txt")

The xmlDoc code is from when I was led to believe at one point that it's not saving because xmlFile was in use (I've been trying a lot different things!).

But, as noted in the code, neither of those work. This is very similar to what I see all over for examples of how to do this, but when I run the program it doesn't change the file at all.

marky
  • 4,878
  • 17
  • 59
  • 103
  • Your loading from one place and saving to another - why? – OneFineDay Feb 05 '15 at 18:56
  • Can you explain what you see that I'm doing wrong, please? – marky Feb 05 '15 at 19:01
  • You can't modify an embedded resource at runtime, end of story. – The Blue Dog Feb 05 '15 at 19:17
  • `you cannot save back to compiled resources at which point you might as well create the file elsewhere to begin with` [as before](http://stackoverflow.com/questions/28348105/embedding-and-referencing-xml-file-in-vb-net-project#comment45045154_28348105) – Ňɏssa Pøngjǣrdenlarp Feb 05 '15 at 19:19
  • It was my understanding that the best way to include a file with a published program was to include it as an embedded resource. Okay, then how do I include the xml file with the published program such that it can be modified at run time based on user input? – marky Feb 05 '15 at 19:21
  • Your installer can include the file in the exe directory. Call if from there. – OneFineDay Feb 05 '15 at 19:24
  • it is *much* easier to just serialize a class and save it to a standard location (maybe 5 lines of code). you wont have the overhead of working with XML nodes either – Ňɏssa Pøngjǣrdenlarp Feb 05 '15 at 19:27

1 Answers1

0

You cannot modify an embedded resource. You can include the XML file as content and it will appear in your build folder as a normal file that can be loaded and updated using the File.IO namespace or XMLDocument.Load().

One catch to look out for is that a file in the Program Files folder being modified will require administrator rights which a user may not have. If this is the case, it's best to copy the file to the AppData folder.

Lance
  • 611
  • 6
  • 28
  • Thanks, Lance. I found an SO question (http://stackoverflow.com/questions/940025/adding-files-to-a-click-once-deployment) that explains how to include a file in a ClickOnce. I set the file as Build Action = "Content" and "Copy if newer". So it's not an embedded resource anymore, but changes still aren't being saved. – marky Feb 05 '15 at 20:12
  • I haven't used ClickOnce so I'm not familiar with where it installs files, is the `settings.txt` file in a directory that requires administrator permissions? – Lance Feb 05 '15 at 20:18
  • `Private xmlFile As New XmlDocument()` in the general declaration area, and I WAS using `xmlFile.LoadXml(My.Resources.Settings)` when it was an embedded resource, but that's changed now. Then in the code I have been using XmlDocument.Load() and .Save() – marky Feb 05 '15 at 20:18
  • I guess I just need to find out the location of the xml file after deployment, relative to the executable. – marky Feb 05 '15 at 20:24