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.ConfigurationImports System.IOImports System.Xml.SerializationImports SNAP.GlobalVarsPublic 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 FunctionEnd ModulePublic 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 StringEnd Class
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
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.
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.
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.