Help Saving to XML Files

  • 12 years ago


    I have got the following code which allows me to save textbox input to an XML file based on date selected. Copy the code into a blank Windows Application if you want to see it in action.

    What I want to do is add another textbox and save that to a separate XML File.

    I've been told the code I have got so far only supports a single key and a single value and therefore it won't take additional textboxes. What I don't know is how to change my code to allow additional textboxes.

    Can you please help me with this?


    Public Class Form1

    Dim DataDictionary As Dictionary(Of String, String)

    Dim DataDocument As Xml.XmlDocument

    Dim FilePath As String

    Dim CurrentDate As Date

    Friend WithEvents DateSelector As System.Windows.Forms.MonthCalendar

    Friend WithEvents DateCombo As System.Windows.Forms.ComboBox

    Friend WithEvents DateInfo As System.Windows.Forms.TextBox

    Public Sub New()


    'Manually add the controls needed

    Me.Text = "LogMyJog version 1"

    Me.Size = New Size(800, 600)

    Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle

    DateSelector = New MonthCalendar()

    DateSelector.Location = New Point(0, 0)


    DateCombo = New ComboBox()

    DateCombo.DropDownStyle = ComboBoxStyle.DropDownList

    DateCombo.Location = New Point(0, 164)

    DateCombo.Width = Me.ClientSize.Width

    DateCombo.Size = New Size(180, 50)


    DateInfo = New TextBox()

    DateInfo.Multiline = True

    DateInfo.ScrollBars = ScrollBars.Vertical

    DateInfo.Location = New Point(0, 188)

    DateInfo.Size = New Size(500, 200)


    CurrentDate = New Date

    DataDictionary = New Dictionary(Of String, String)

    DataDocument = New Xml.XmlDocument()

    'This is where the data will be stored

    FilePath = Application.StartupPath & "\dateinfo.xml"

    If FileIO.FileSystem.FileExists(FilePath) Then

    DataDocument.Load(Application.StartupPath & "\dateinfo.xml")

    Dim Items As Xml.XmlNodeList

    Dim Item As Xml.XmlNode

    Dim ItemDate As Date

    Items = DataDocument.DocumentElement.SelectNodes("//item")

    For Each Item In Items

    'Add data into the DataDictionary and ComboBox

    ItemDate = Date.Parse(Item.Attributes("date").Value)

    DataDictionary.Add(ItemDate.Date.ToLongDateString(), Item.FirstChild.InnerText)



    End If

    'Add the event handlers for the three controls

    AddHandler DateInfo.TextChanged, AddressOf Me.DateInfo_TextChanged

    AddHandler DateCombo.SelectedIndexChanged, AddressOf Me.DateCombo_SelectedIndexChanged

    AddHandler DateSelector.DateChanged, AddressOf Me.DateSelector_DateChanged

    'Initialize the controls using the CurrentDate


    End Sub

    Private Sub DateSelect()

    If DateSelector.SelectionStart.Date = DateSelector.SelectionEnd.Date Then

    CurrentDate = DateSelector.SelectionStart.Date

    If Not DateCombo.Items.Contains(CurrentDate.Date.ToLongDateString()) Then


    DataDictionary.Add(CurrentDate.Date.ToLongDateString(), "")

    DateCombo.SelectedItem = CurrentDate.Date.ToLongDateString()


    DateCombo.SelectedItem = CurrentDate.Date.ToLongDateString()

    If DataDictionary.ContainsKey(CurrentDate.Date.ToLongDateString()) Then

    DateInfo.Text = DataDictionary.Item(CurrentDate.Date.ToLongDateString())


    DateInfo.Text = ""

    End If

    End If

    End If

    End Sub

    Private Sub DateSelector_DateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs)


    End Sub

    Private Sub DateCombo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    If DataDictionary.ContainsKey(DateCombo.Text) Then

    DateSelector.SelectionStart = Date.Parse(DateCombo.Text)

    DateSelector.SelectionEnd = Date.Parse(DateCombo.Text)

    End If

    End Sub

    Private Sub DateInfo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    If DataDictionary.ContainsKey(CurrentDate.Date.ToLongDateString()) Then

    DataDictionary.Item(CurrentDate.Date.ToLongDateString()) = DateInfo.Text

    End If

    End Sub

    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing

    'Save the DataDictionary to the Xml Document...

    DataDocument = New Xml.XmlDocument()

    Dim Root As Xml.XmlElement

    Dim Item As Xml.XmlElement

    Dim Data As Xml.XmlCDataSection

    Dim DateAttribute As Xml.XmlAttribute

    Dim Key As String

    Root = DataDocument.CreateElement("data")

    For Each Key In DataDictionary.Keys

    Item = DataDocument.CreateElement("item")

    DateAttribute = DataDocument.CreateAttribute("date")

    DateAttribute.Value = Key


    Data = DataDocument.CreateCDataSection(DataDictionary(Key))






    End Sub

    End Class


Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” - Rick Osborne