MrNoobertNobody
MrNoobertNobody

Reputation: 3

VB.NET, XML, Write and Save with inner text and attributes

I've got a basic program going to automate some things on my home computer. I'm using Visual Studio Community edition 2019 in VB.NET

my xml looks similar to this

<data>
    <settings topbar="gray"></settings>
    <settings bottombar="gray"></settings>
    <settings font="arial"></settings>
    <settings otherstuff="moresettings"></settings>
    <backup location="c:\folder1">fave folder</backup>
    <backup location="c:\folder2">not fave folder</backup>
    <backup location="c:\folder3">another folder</backup>
</data>

I've already got everything else working. I can read the settings and perform the backups without problems but I wanted a way for the program to insert data into the xml document without me typing it out manually. Less mistakes can be made with FolderBrowserDialog() than typing.

The first half of my code is working fine but I wanted to add that too, just incase it needs re-writing to make the rest work. This is setup on a basic button

If txt_xml_title.Text = "" Then
            MessageBox.Show("Title cannot be blank", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If
        If txt_xml_folder.Text = "" Then
            MessageBox.Show("Location not specified", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        Dim xDoc As New XmlDocument()
        xDoc.Load(Application.StartupPath & "/xml.xml")
        Dim nodes As XmlNodeList = xDoc.DocumentElement.SelectNodes("/data/backup")
        For Each node As XmlNode In nodes
            If node.InnerText = txt_xml_title.Text Then
                MessageBox.Show(txt_xml_title.Text & " is already declared, please rename the item", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End If
        Next

now this is where I have fumbled up so many times and re-wrote it more times than I can count. (to be honest, I don't really understand anything anymore. I've seen some answers on here for C# but that is beyond me completely. I'm just a small time VB.NET noob :)


        Dim nodeTitle As XmlNode = xDoc.CreateElement("backup")
        Dim nodeAtribute As XmlNode = xDoc.CreateAttribute("location")

        xDoc.CreateNode(XmlNodeType.Element, "backup", "location")
        nodeTitle.InnerText = txt_xml_title.Text
        nodeAtribute.InnerText = txt_xml_folder.Text
        xDoc.AppendChild(nodeTitle)
        xDoc.AppendChild(nodeAtribute)


        xDoc.Save(Application.StartupPath & "/xml.xml")

If I'm barking up the wrong tree, how would I be able to add a new line that would create

<backup location="txt_xml_location.text">txt_xml_title.text</backup>

Thank you in advance from a noob :)

Upvotes: 0

Views: 2225

Answers (1)

Xingyu Zhao
Xingyu Zhao

Reputation: 625

According to your xml file, you need to add the child element under the root element, so use XmlDocument.DocumentElement Property to get the root XmlElement for the document:

Dim nodeTitle As XmlElement = xDoc.CreateElement("backup")
'...
xDoc.DocumentElement.AppendChild(nodeTitle)

The whole code looks like:

    If txt_xml_title.Text = "" Then
        MessageBox.Show("Title cannot be blank", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
    End If
    If txt_xml_folder.Text = "" Then
        MessageBox.Show("Location not specified", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
    End If

    Dim xDoc As New XmlDocument()
    xDoc.Load(Application.StartupPath & "/xml.xml")
    Dim nodes As XmlNodeList = xDoc.DocumentElement.SelectNodes("/data/backup")
    For Each node As XmlNode In nodes
        If node.InnerText = txt_xml_title.Text Then
            MessageBox.Show(txt_xml_title.Text & " is already declared, please rename the item", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If
    Next

    Dim nodeTitle As XmlElement = xDoc.CreateElement("backup")
    nodeTitle.SetAttribute("location", txt_xml_folder.Text)
    nodeTitle.InnerText = txt_xml_title.Text
    xDoc.DocumentElement.AppendChild(nodeTitle)

    xDoc.Save(Application.StartupPath & "/xml.xml")

Upvotes: 1

Related Questions