Avatar of Tony Gardner
Tony Gardner
Flag for United States of America asked on

Problem Writing Multiple Values to XML File via XMLWriter in VB.NET

Hello Experts.

I've been working on learning how to read and write from/to a custom XML file within my vb.net project.

At this point, it works, but only knows how to replace the entire file on the write operation. Let me explain...

I have a Public Class UserPrefs which iterates eight items that the user can maintain via checkboxes on a small dialog. Let's say for the moment that if the user clicks "AutoSelect", my setXML subroutine successfully creates the UserPreferences.xml file in bin/debug with the one line <AutoSelect>True</AutoSelect>. Next, the user unchecks "Countdown", so my setXML happily re-creates UserPreferences.xml with the line <Countdown>False</Countdown> sending AutoSelect to bit heaven. My desire is that I can keep track of all eight items. Any thoughts on what I might be doing wrong?

Imports System.Configuration
Imports System.IO
Imports System.Xml.Serialization
Imports SNAP.GlobalVars

Public Module iXML

    Sub setXML(ByVal SetKey As String, ByVal SetVal As String)
            Dim xmlFile As New StreamWriter(xmlPath)
            Dim xmlWriter As New XmlSerializer(GetType(UserPrefs))
            Dim xmlSetKey As New UserPrefs
            Select Case SetKey
                Case "AutoSelect" : xmlSetKey.AutoSelect = SetVal
                Case "Countdown" : xmlSetKey.Countdown = SetVal
                Case "LagPrompt" : xmlSetKey.LagPrompt = SetVal
                Case "MyTeam" : xmlSetKey.MyTeam = SetVal
                Case "NoMatches" : xmlSetKey.NoMatches = SetVal
                Case "SkipIntro" : xmlSetKey.SkipIntro = SetVal
                Case "SwapPlayers" : xmlSetKey.SwapPlayers = SetVal
                Case "GeneralHelp" : xmlSetKey.GeneralHelp = SetVal
            End Select
            xmlWriter.Serialize(xmlFile, xmlSetKey)
        End Try
    End Sub

    Public Function getXML(ByVal GetKey As String)
        If Not (File.Exists(xmlPath)) Then Return Nothing
        Dim xmlRtnVal As String = ""
            Dim xmlFile As New StreamReader(xmlPath)
            Dim xmlReader As New XmlSerializer(GetType(UserPrefs))
            With CType(xmlReader.Deserialize(xmlFile), UserPrefs)
                Select Case GetKey
                    Case "AutoSelect" : xmlRtnVal = .AutoSelect
                    Case "Countdown" : xmlRtnVal = .Countdown
                    Case "LagPrompt" : xmlRtnVal = .LagPrompt
                    Case "MyTeam" : xmlRtnVal = .MyTeam
                    Case "NoMatches" : xmlRtnVal = .NoMatches
                    Case "SkipIntro" : xmlRtnVal = .SkipIntro
                    Case "SwapPlayers" : xmlRtnVal = .SwapPlayers
                    Case "GeneralHelp" : xmlRtnVal = .GeneralHelp
                End Select
            End With
        End Try
        Return xmlRtnVal
    End Function

End Module

Public Class UserPrefs
    Public AutoSelect As String
    Public Countdown As String
    Public LagPrompt As String
    Public MyTeam As String
    Public NoMatches As String
    Public SkipIntro As String
    Public SwapPlayers As String
    Public GeneralHelp As String
End Class

Open in new window

And the subroutine that calls it:
    Private Sub chkQuikTip1_CheckedChanged(sender As Object, e As EventArgs) Handles _
        chkQuikTip1.CheckedChanged, chkQuikTip2.CheckedChanged, chkQuikTip3.CheckedChanged, chkQuikTip4.CheckedChanged,
         chkQuikTip5.CheckedChanged, chkQuikTip6.CheckedChanged, chkQuikTip7.CheckedChanged, chkQuikTip8.CheckedChanged
        Dim WhatWasChecked As String = sender.Tag
        If WhatWasChecked IsNot Nothing Then
            Call setXML(WhatWasChecked, sender.Checked.ToString)
        End If
    End Sub

Open in new window

Visual Basic.NETXML

Avatar of undefined
Last Comment
Tony Gardner

8/22/2022 - Mon

Your culprit it the following:             Select Case SetKey

Because of how you have coded things you only ever write one setting to the file, all others are thrown away.  You need to set ALL of your settings by writing each and every one of them to the file, not just the one changed.

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Tony Gardner

Sorry for the long wait, gentlemen. To be perfectly honest, implementing XML read/write has been shelved until I can sort out some more pressing matters.

Granting points based on contribution.

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes