C
cj2
I'm putting together an example of how to read elements from XML for a
colleague and I can't get it to work with string values. I've used this
method for years but apparently everything has been int32. How come my
string isn't working?
Private Sub Button46_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button46.Click
'Loading config file settings
Dim cfgFile As String = "c:\test.cfg"
Dim cfgDoc As New Xml.XmlDocument
Dim setting1element As Xml.XmlNodeList
Dim setting2element As Xml.XmlNodeList
Dim setting1 As Int32
Dim setting2 As String
Try
cfgDoc.Load(cfgFile)
setting1element = cfgDoc.GetElementsByTagName("Setting_1")
setting2element = cfgDoc.GetElementsByTagName("Setting_2")
setting1 = Val(setting1element(0).InnerText)
setting2 = Val(setting2element(0).InnerText)
Catch
'error in config file use default settings
Try
cfgDoc.LoadXml("<Test_config_file>" & _
" <Program_settings>" & _
" <Setting_1>1000</Setting_1>" & _
" <Setting_2>This is a
test!</Setting_2>" & _
" </Program_settings>" & _
"</Test_config_file>")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Try
setting1element = cfgDoc.GetElementsByTagName("Setting_1")
setting2element = cfgDoc.GetElementsByTagName("Setting_2")
setting1 = Val(setting1element(0).InnerText)
setting2 = Val(setting2element(0).InnerText).ToString
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Try
setting1 = InputBox("Need Input...", "Enter a number ", setting1)
setting2 = InputBox("Need Input...", "Enter a string ", setting2)
'Saving config file settings
setting1element(0).InnerText = setting1
setting2element(0).InnerText = setting2
Try
cfgDoc.Save(cfgFile)
Catch
End Try
End Sub
colleague and I can't get it to work with string values. I've used this
method for years but apparently everything has been int32. How come my
string isn't working?
Private Sub Button46_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button46.Click
'Loading config file settings
Dim cfgFile As String = "c:\test.cfg"
Dim cfgDoc As New Xml.XmlDocument
Dim setting1element As Xml.XmlNodeList
Dim setting2element As Xml.XmlNodeList
Dim setting1 As Int32
Dim setting2 As String
Try
cfgDoc.Load(cfgFile)
setting1element = cfgDoc.GetElementsByTagName("Setting_1")
setting2element = cfgDoc.GetElementsByTagName("Setting_2")
setting1 = Val(setting1element(0).InnerText)
setting2 = Val(setting2element(0).InnerText)
Catch
'error in config file use default settings
Try
cfgDoc.LoadXml("<Test_config_file>" & _
" <Program_settings>" & _
" <Setting_1>1000</Setting_1>" & _
" <Setting_2>This is a
test!</Setting_2>" & _
" </Program_settings>" & _
"</Test_config_file>")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Try
setting1element = cfgDoc.GetElementsByTagName("Setting_1")
setting2element = cfgDoc.GetElementsByTagName("Setting_2")
setting1 = Val(setting1element(0).InnerText)
setting2 = Val(setting2element(0).InnerText).ToString
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Try
setting1 = InputBox("Need Input...", "Enter a number ", setting1)
setting2 = InputBox("Need Input...", "Enter a string ", setting2)
'Saving config file settings
setting1element(0).InnerText = setting1
setting2element(0).InnerText = setting2
Try
cfgDoc.Save(cfgFile)
Catch
End Try
End Sub