troubleshooting Question

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

Avatar of Tony Gardner
Tony GardnerFlag for United States of America asked on
Visual Basic.NETXML
3 Comments1 Solution144 ViewsLast Modified:
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
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
ASKER CERTIFIED SOLUTION
ste5an
Senior Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros