xmlvb.net

Add multiple entries in XML File


I have a form containing a TextBox and a RichTextBox. The RichTextBox contains text to be edited by the user and the TextBox contains the descriptive name for the text. I want to save the RichTextBox text with the name describing it in an XML File. I am using the following code to do this.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button8.Click
    Dim filename As String = "C:\Users\User\Desktop\Test.xml"
    Dim doc As XDocument = Nothing
    Dim RTBText As String = RichTextBox1.Text
    Dim TextBoxText As String = TextBox1.Text
    createNode(TextBoxText, RTBText, doc)
    doc.Save(filename)
End Sub

Private Sub createNode(ByVal varname As String, ByVal varvalue As String, ByRef doc As XDocument)
    Dim identification As String = "<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?><Log></Log>"
    doc = XDocument.Parse(identification)
    Dim EntryData As XElement = doc.FirstNode
    Dim EntryDate = System.DateTime.Today.ToString("ddMMyy")
    Dim variables As New XElement("Entry", New Object() {
                                  New XElement("EntryDescription", varname),
                                  New XElement("EntryValue", varvalue),
                                  New XElement("EntryDate", EntryDate)
                                  })
    EntryData.Add(variables)

End Sub

The code produces the following XML File.

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Log>
  <Entry>
    <EntryDescription>This is the TextTextbox Text</EntryDescription>
    <EntryValue>This is RTB Data</EntryValue>
    <EntryDate>181024</EntryDate>
  </Entry>
</Log>

The file will not have more than a few hundred entries and this seems to be fine but I can't find out how to add additional entries to the file. As it is, the next entry just overwrites the existing entry. When complete, I need to be able to search the XML file's descriptive names and then retrieve the accompanying text. Please could someone assist in finding a solution that will enable me to add additional entries.


Solution

  • I prefer to use XElement and LINQ,

    Public Class Form1
    
        Private doc As XElement = Nothing
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim filename As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            filename = IO.Path.Combine(filename, "FOOtest.xml")
            If doc Is Nothing Then
                If IO.File.Exists(filename) Then
                    doc = XElement.Load(filename) 'open file if exists
                Else
                    doc = <log></log> 'create new
                    doc.Save(filename)
                End If
            End If
            createNode(TextBox1.Text, RichTextBox1.Text) 'add node
            doc.Save(filename) 'and save
        End Sub
    
        Private Sub createNode(varname As String,
                                varvalue As String)
            Dim EntryDate As String = System.DateTime.Now.ToString("ddMMyy")
            Dim variables As XElement
            'build a new entry
            variables = <Entry>
                            <EntryDescription><%= varname %></EntryDescription>
                            <EntryValue><%= varvalue %></EntryValue>
                            <EntryDate><%= EntryDate %></EntryDate>
                        </Entry>
            doc.Add(variables) 'add to existing
        End Sub
    End Class
    

    edit - another approach

    Public Class Form1
    
        Private doc As XElement = Nothing
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim filename As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            filename = IO.Path.Combine(filename, "FOOtest.xml")
            If doc Is Nothing Then
                If IO.File.Exists(filename) Then
                    doc = XElement.Load(filename) 'open file if exists
                Else
                    doc = <log></log> 'create new
                    doc.Save(filename)
                End If
            End If
            doc.Add(createEntryNode(TextBox1.Text, RichTextBox1.Text)) 'add node
            doc.Save(filename) 'and save
        End Sub
    
        Private Function createEntryNode(varname As String,
                                          varvalue As String) As XElement
            Dim EntryDate As String = System.DateTime.Now.ToString("ddMMyy")
            Dim variables As XElement
            'build a new entry
            variables = <Entry>
                            <EntryDescription><%= varname %></EntryDescription>
                            <EntryValue><%= varvalue %></EntryValue>
                            <EntryDate><%= EntryDate %></EntryDate>
                        </Entry>
            Return variables
        End Function
    End Class