Click here to Skip to main content
15,908,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've created an xml file and want the elements to populate a combobox but have hit dead ends constantly. I haven't really tried any code since I'm not sure where to start for it, at this point I'm just wondering if you're able to. I've posted the xml and what the elements I want to be able to put into the combobox if that helps at all. Thanks.

What I have tried:

VB.NET
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Try
            If IO.File.Exists("contactlist.xml") Then
                Dim xmlDoc As New XmlDocument()
                xmlDoc.Load("contactlist.xml")

                Dim mainNode As XmlNode = xmlDoc.DocumentElement
                Dim newOp As XmlNode = xmlDoc.CreateElement("contact")

                'Create elements and append them to the main document node'
                Dim subNode As XmlNode = xmlDoc.CreateElement("name")
                subNode.InnerText = txtName.Text
                newOp.AppendChild(subNode)

                subNode = xmlDoc.CreateElement("address")
                subNode.InnerText = txtAddress.Text
                newOp.AppendChild(subNode)

                subNode = xmlDoc.CreateElement("place")
                subNode.InnerText = txtPlace.Text
                newOp.AppendChild(subNode)

                subNode = xmlDoc.CreateElement("phone")
                subNode.InnerText = txtPhone.Text
                newOp.AppendChild(subNode)

                subNode = xmlDoc.CreateElement("email")
                subNode.InnerText = txtEmail.Text
                newOp.AppendChild(subNode)

                'append child node to main node and save'
                mainNode.AppendChild(newOp)
                xmlDoc.Save("contactlist.xml")

            Else

                Dim XmlSet As New XmlWriterSettings()
                XmlSet.Indent = True

                ' Initialize the XmlWriter.
                Dim XmlWrite As XmlWriter = XmlWriter.Create("contactlist.xml", XmlSet)

                With XmlWrite

                    ' create the XML file
                    .WriteStartDocument()
                    .WriteComment("XML Database.")
                    .WriteStartElement("Contacts")

                    .WriteStartElement("contact")

                    ' write the tags and the entry into the tags
                    .WriteStartElement("name")
                    .WriteString(txtName.Text.ToString())
                    .WriteEndElement()

                    .WriteStartElement("address")
                    .WriteString(txtAddress.Text.ToString())
                    .WriteEndElement()

                    .WriteStartElement("place")
                    .WriteString(txtPlace.Text.ToString())
                    .WriteEndElement()

                    .WriteStartElement("phone")
                    .WriteString(txtPhone.Text.ToString())
                    .WriteEndElement()

                    .WriteStartElement("email")
                    .WriteString(txtEmail.Text.ToString())
                    .WriteEndElement()

                    ' close entry
                    .WriteEndElement()
                    .WriteEndDocument()
                    .Close()

                End With
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try
Posted
Updated 22-Jun-21 20:29pm
v2

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900