VB.Net | Appending an existing XML file

VB.Net | Appending an existing XML file

XML is widely used by .net oriented programming. It is vast and learning XML could be requiring loads of dedication. I needed to add a XML file to hold certain configuration details that could be read with one of the VB.Net projects currently I am busy with. This project deals with running Oracle VirtualBox VM(Virtual Machines) headless using a Windows Service as Local System so that multiple administrators could log in and log off from the Windows Server that hosts Virtual machines without closing down the machines abrupt (This project is ongoing, half cooked)

Without wasting more time, let us see how to append an “existing” XML file using VB.Net

Create a Windows Form Project
Create a XML file (MyVMS.xml) using your favorite text editing tool and copy the below

[code language=”xml” gutter=”false”]
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<VM>
<MACHINE>
<MACHINE_ID>1</MACHINE_ID>
<VM_NAME>MyVM</VM_NAME>
<VM_SHUTDOWNMODE>poweroff</VM_SHUTDOWNMODE>
</MACHINE>
</VM>
[/code]
and copy the XML file to the project root folder

Add the namespaces to your new project

[code language=”vb” gutter=”false”]
Imports System.Xml
Imports System.Data
[/code]

As I said in the beginning, we will be adding data to an “existing” XML file, hence the XML files must have a root element, for example

[code language=”xml” gutter=”false”]
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<VM>
</VM>
[/code]

Drag a button to your forms1.vb design view
Drag another button to your forms1.vb design view
Drag a DataGridView to your forms1.vb design view

Now we will try to add one more “MACHINE” node into the existing XML file using VB.Net code block

[code language=”vb” gutter=”false”]
Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim xmldoc As New XMLDocument()
Try
xmldoc.Load("MyVMS.xml")
Dim e1 As XmlElement = xmldoc.CreateElement("MACHINE")
Dim e2 As XmlElement = xmldoc.CreateElement("MACHINE_ID")
e2.InnerText = "2"
Dim e3 As XmlElement = xmldoc.CreateElement("VM_NAME")
e3.InnerText = "Linux"
Dim e4 As XmlElement = xmldoc.CreateElement("VM_SHUTDOWNMODE")
e4.InnerText = "savestate"

e1.AppendChild(e2)
e1.AppendChild(e3)
e1.AppendChild(e4)

xmldoc.DocumentElement.AppendChild(e1)
xmldoc.Save("MyVMS.xml")

Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim xmlFile As XmlReader
xmlFile = XmlReader.Create("MyVMS.xml", New XmlReaderSettings())
Dim ds As New DataSet
ds.ReadXml(xmlFile)
DataGridView1.DataSource = ds.Tables(0)
DataGridView1.Columns(0).HeaderText = "Id"
DataGridView1.Columns(1).HeaderText = "VM Name"
DataGridView1.Columns(2).HeaderText = "Shutoff Mode"

End Sub
End Class
[/code]

Now, as you click on the first button, a new node for “MACHINE” will be inserted using the static information attached & by activating the second button, your data grid view should populate the MACHINE information already stored within the XML file.

Pretty simple & easy, right? You may rush ahead and say thanks to my dearest friend and ONE of the best .Net programmers Aziz Marafi / @azizane , who had coded majority of the block above for the “FileMyster” project.

Please post your comments and queries and I may learn few more things with you!

regards,

rajesh

Advertisements

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.