Question Sharing settings XML document between forms

duphus

Member
Joined
Dec 26, 2009
Messages
20
Programming Experience
Beginner
I have an application built that has several forms. I use an XML file to store settings data (mainly folder names for data output) and I need each form to be able to access and modify the data. I'd also like each form to access functions from a main form (such as file parsing functions). I have read articles about properties and inheritance but I need some guidance as this topic is pretty confusing.

What I've been able to get to work is this

VB.NET:
Public ReadOnly Property UserName() As String
        Get
            Return TextBox1.Text
        End Get
    End Property

    Public ReadOnly Property AssayCode() As String
        Get
            Return ComboBox1.Text.
        End Get
    End Property

    Public Property AssaySettings As XDocument
        Get
            Return XDocument.Load("C:\assaysettings.xml"))
        End Get
        Set(ByVal value As XDocument)
            XDocument.Save("C:\assaysettings.xml")) = value 'ERROR
        End Set
    End Property

I put this code on my start form (StartForm). However, I am not able to save the XML file using this procedure (an error occurs). I'm not sure if I need to make a separate class or how I would be able to save settings to the file from each form. Please advise.
 
what is the exception generated?
 
at
VB.NET:
XDocument.Save("C:\assaysettings.xml")) = value
Apparently you cannot set properties this way. There probably is a better way of doing this.
 
what i meant was what kind of error did you encounter?

Like System.IOException, System.NullReferenceException
 
When you load an XML file into a XmlDocument it is a in memory object that you can read,change,remove,add,etc... the elements. Then if you change something you must save it, or it will do nothing. It has no .Dispose method so it's lifetime is limited to it's scope, but I like to set it to Nothing when done. So any form can load this file and parse it, so like sharing - yes.
 
So you're saying I have to load and save every time I want to get the setting from the xml file? Seems like there would be a way to do this by writing a separate class to handle loading, saving, etc.

I came across this in my research
CodeProject: Share User Settings Between Applications. Free source code and programming help
and this would be ideal. I don't know how to implement it into my project though, especially since I'd have to convert the code from C#.
 
So the project I listed above gave me some ideas and was a good starting point for research but I found it too difficult to adopt and follow.

What I basically need is a class that would let me obtain or set elements from an xml file based on just passing an attribute. For example

SettingsClass.GetSetting("attributevalue")

Should I use properties in the new class? And where would I place the code to obtain the values from the file?
 
How about using the built in My.Settings Object, see the related links for user settings.
 
No I tried that, the built in settings are not adequate because i have several input/output folders that need to be accessed and therefore it would amount to too many settings.
 
Back
Top