Problem writing self closing null value to SettingElement in app.config

Phill 61 Reputation points
2022-06-04T21:58:09.71+00:00

I am using the following code to write/edit the app.config file at runtime that allows the user to change ApplicationSettings however, I have a problem when writing pr saving null values.

Here is the code:

            Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)  
            Dim applicationSectionGroup As ConfigurationSectionGroup = config.GetSectionGroup("applicationSettings")  
  
            Dim strAppName As String = System.Reflection.Assembly.GetExecutingAssembly.GetName().Name  
            Dim applicationConfigSection As ConfigurationSection = applicationSectionGroup.Sections(strAppName & ".My.MySettings")  
            Dim clientSection As ClientSettingsSection = CType(applicationConfigSection, ClientSettingsSection)  
  
            ' set a value to that specific property  
            Dim applicationSetting As SettingElement  
  
            applicationSetting = clientSection.Settings.Get("Testsetting")  
            applicationSetting.Value.ValueXml.InnerText = Nothing  
            
            ' without this, saving won't work  
            applicationConfigSection.SectionInformation.ForceSave = True  
            ' save  
            config.Save()  

The problem is, when I save null values with this code, the Value tag is not written correctly. It's writing the tag as follows:
208379-capture2.jpg

The problem is, when I read the tag using My.Settings.Testsetting, it's being read by vb.net as multiple spaces instead of a null or empty string. I know I can use a trim statement to read the settings and clear the spaces but, I would rather find a way to save the empty value correctly instead of working around the problem.

In my production code, I am passing the Value in as a string but for testing purposes above, I have been trying to find a way to save it as a self-closing value like
<value />
208367-capture.jpg

That's how visual studio saves the tag with a null value when saved from the Project Property Settings screen.

Any help would be really appreciated.

Thanks

Developer technologies | VB
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-06-05T14:56:26.977+00:00

    I wrote the following in C#, converted to VB.

    My.Settings.Default.Testsetting = ""
    My.Settings.Default.Save()
    Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
    config.AppSettings.Settings.Remove("Testsetting")
    config.Save()
    

    Result

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <userSettings>
            <DataBindValidate.Properties.Settings>
                <setting name="Testsetting" serializeAs="String">
                    <value />
                </setting>
            </DataBindValidate.Properties.Settings>
        </userSettings>
    </configuration>
    

  2. Phill 61 Reputation points
    2022-06-06T06:17:54.467+00:00

    After a LOT of late nights and unpaid OT this weekend, I found the solution.

    The following code allows me to change how the Value Node is written in the app.config (.exe.config) file when the value is null or an empty string.

    Public Function WriteApplicationSettingValue(strSetting As String, strValue As String) As Boolean
    
            Try
                ' this gets the applicationSettings section (and the inner section 'inoBIBooks.My.MySettings')
                Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
                Dim applicationSectionGroup As ConfigurationSectionGroup = config.GetSectionGroup("applicationSettings")
    
                Dim strAppName As String = System.Reflection.Assembly.GetExecutingAssembly.GetName().Name
                Dim applicationConfigSection As ConfigurationSection = applicationSectionGroup.Sections(strAppName & ".My.MySettings")
                Dim clientSection As ClientSettingsSection = CType(applicationConfigSection, ClientSettingsSection)
    
                ' set a value to that specific property
                Dim applicationSetting As SettingElement = clientSection.Settings.Get(strSetting)
    
                If Not IsNothing(applicationSetting) Then
                    'saving empty strings causes the <VALUE> node/tag to be written incorrectly in the app.config file incorrently
                    'The <Value> and <Value /> are written on 2 different lines which when read back through My.Settings causes
                    'the vealue appears as "       " instead of "".  To overcome this problem, we have to create our own
                    'XML node and override the XMLValue node in the SettingElement
                    If String.IsNullOrEmpty(strValue) Then
                        Dim doc As New XmlDocument()
                        doc.LoadXml("<value />")
    
                        Dim SelfClosingValueNode As System.Xml.XmlNode = doc.SelectSingleNode("value")
                        applicationSetting.Value.ValueXml = SelfClosingValueNode
                    Else
                        applicationSetting.Value.ValueXml.InnerText = strValue
                    End If
    
                    ' without this, saving won't work
                    applicationConfigSection.SectionInformation.ForceSave = True
                    ' save
                    config.Save()
    
                    Return True
                Else
                    Return False
                End If
    
            Catch ex As Exception
                MsgBox("The following error occurred while attempting to write to the application settings file." & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Exclamation)
                Return False
            End Try
    
        End Function
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.