Link to home
Start Free TrialLog in
Avatar of Tony Gardner
Tony GardnerFlag 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)
        Try
            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)
            xmlFile.Close()
        Catch
        End Try
    End Sub

    Public Function getXML(ByVal GetKey As String)
        If Not (File.Exists(xmlPath)) Then Return Nothing
        Dim xmlRtnVal As String = ""
        Try
            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
        Catch
        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

Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

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.
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tony Gardner

ASKER

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.

Cheers,
Tony