Hello all,

I have a problem with a program i'm trying to write.
I only need 1 more thing to make it work properly, the textboxes that are filled in must be added or overwritten in an existing XML file.

I know i need to work with XMLWriter, but i have no idea about the code.

This is what i have right now.

'Set up the XmlWriter settings.
         Dim settings As New XmlWriterSettings()
         settings.Indent = True

        ' Initialize the XmlWriter.
         Dim XmlWrt As XmlWriter = XmlWriter.Create(Application.StartupPath & "XmlTest.xml", settings)

         With XmlWrt
            ' Write the Xml declaration.
            .WriteStartDocument()

            ' Write the root element.
            .WriteStartElement("clients")

            ' Start our first Client.
            .WriteStartElement("clients")
            ' Give this person the attribute ID of 1.
            .WriteAttributeString("ID", "1")

            ' The person nodes.
            .WriteStartElement("name")
            .WriteValue(strKlant)
            .WriteEndElement()

            .WriteStartElement("sender")
            .WriteValue(strSender)
            .WriteEndElement()

            .WriteStartElement("errorwords")
            .WriteValue(strError)
            .WriteEndElement()

            .WriteStartElement("falsewords")
            .WriteValue(strFalse)
            .WriteEndElement()

            .WriteStartElement("link")
            .WriteValue(strLink)
            .WriteEndElement()

            .WriteStartElement("days")
            .WriteValue(strDays)
            .WriteEndElement()

            .WriteStartElement("active")
            .WriteValue(strActive)
            .WriteEndElement()

            ' The end of this person.
            .WriteEndElement()


            ' Close the XmlWriter.
            .WriteEndDocument()
            .Close()

Can someone please help me?

Your code appears to work as is. You'll have to describe more fully what the specific problem is

The problem is that i don't know how to get it into the xml that it is originally loaded in.

This is only to 'write it', but i need to save it.

Yes. So far I can open an xml with the form loading, i can read information from it bij selecting a client in a combobox. But now whenever I have to change information or add a new client, it has to be saved inside the same xml I opened earlier.

Edited 1 Year Ago by Extorza

Ok, I found the code on how to add new clients to the xml and save. Only now I need to be able to save the modified txtboxes into the opened XML.

Private Sub btnOpslaan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpslaan.Click


        Dim doc As New XmlDocument()
        doc.Load(THEFILE)
        Dim pName, pSender, pErrorwords, pFalsewords, pLink, pDays, pActive As String

        pName = naamtextbox.Text
        pSender = sendertextbox.Text
        pErrorwords = errorwordstextbox.Text
        pFalsewords = falsewordstextbox.Text
        pLink = linktextbox.Text
        pDays = daystextbox.Text
        pActive = activetextbox.Text







    End Sub

That's all I have right now.

You'll find that using XDocument instead of XmlDocument will work much better:

Private Sub btnOpslaan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpslaan.Click
    Dim info As XDocument = XDocument.Load(THEFILE)
    'Increment last ID value for the new element
    'If the content is to be dynamic with repeated additions and 
    'removals you might want an alternative method that reuses the 
    'missing ID's
    Dim nextID As Integer = Integer.Parse(info.Element("clients").Elements.Last.Attribute("ID").Value) + 1
    info.Element("clients").Add(New XElement("clients", New XAttribute("ID", nextID.ToString)))
    info.Element("clients").Elements.Last.Add(New XElement("name", naamtextbox.Text))
    info.Element("clients").Elements.Last.Add(New XElement("sender", sendertextbox.Text))
    info.Element("clients").Elements.Last.Add(New XElement("errorwords", errorwordstextbox.Text))
    info.Element("clients").Elements.Last.Add(New XElement("falsewords", falsewordstextbox.Text))
    info.Element("clients").Elements.Last.Add(New XElement("link", linktextbox.Text))
    info.Element("clients").Elements.Last.Add(New XElement("days", daystextbox.Text))
    info.Element("clients").Elements.Last.Add(New XElement("active", activetextbox.Text))
    info.Save(fileName)
End Sub

Edited 1 Year Ago by tinstaafl

Thanks for the help. The only problem is that in the file im using there aren't any ID's. This is what the xml looks like. (with more clients ofc.)

<clients>
  <client>
    <name>Test</name>
    <sender>Test</sender>
    <errorWords>Test</errorWords>
    <falseWords>Test</falseWords>
    <link>Test</link>
    <days>MA|DI|WO|DO|VR</days>
    <active>1</active>
  </client>
</clients>

I really don't know how to program, but I do need help.

Edited 1 Year Ago by Extorza

Learning programming is about experimenting. The code I gave you works. Play with it, modify it, adapt it to your needs.

I know it works, it's just that i don't know how to modify the nextID to nextClient.
Probably something like: Dim nextClient as string..... But I have no clue on what to do.

Thank you for your help btw.

The code I submitted does that automatically by adding one to the last client id. If you need some other numbering system that should probably be a different question.

This article has been dead for over six months. Start a new discussion instead.