I've been trying to create a simple scores system for a quiz I'm making in Visual Basic. I started off with a basic XML file like this:
<?xml version="1.0" encoding="utf-8" ?>
<Scores>
<test1>13</test1>
</Scores>
I managed to open the XML file up and print a specific node's inner text and this is my code so far (file path has been omitted):
Imports System.Xml
Module Module1
Sub Main()
Dim test = XDocument.Load("filepath")
Dim test5 As String = test.Descendants("test1").Value()
Console.WriteLine(test5)
Console.ReadLine()
End Sub
End Module
My only problem now is trying to edit the inner text of a specific node. How would I go about doing this?
You can do this easily with an XmlDocument
and XmlNode
.
Imports System.Xml
Module Module1
Sub Main()
Dim xmlDoc As XmlDocument = New XmlDocument
Dim test1Node As XmlNode = Nothing
xmlDoc.Load("filePath.xml")
test1Node = xmlDoc.SelectSingleNode("//Scores/test1")
Console.WriteLine(test1Node.InnerText)
test1Node.InnerText = "42"
xmlDoc.Save("filePath.xml")
Console.ReadLine()
End Sub
End Module