Posting Code Blocks with your free WordPress Blog

I’ve updated this page to reflect the “Project Gutenberg” changes. Currently supported details are available with the below link.

Please visit for detailed information about how to post source code blocks with your free blog.



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"?>
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

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"?>

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()
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"



Catch ex As Exception
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
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

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!



VB.Net | Get IP address of a computer against hostname | Fun projects


How to?

Open Visual Studio IDE (We are using VS 2012 Express and the .Net framework is 4.5 by default, hence if few things do not work at your setups, please cross check the specs we specified) and create a Windows form application. We’ll be mostly doing our samples using forms, a better mechanism to test stuffs against console applications (Minimum from our experiences)

drag a button to the form

drag a listbox to the form

& do not change any property for any of the above items

right click on the form and select view code, once the code page opens
Ctrl+A (Select all and delete)

copy and paste the below code block (replacing the original content of the code page)

[code language=”vb” gutter=”false” wraplines=”true”]
Imports System.Net

Public Class Form1
Public Function getip(ByVal lst As ListBox) As ListBox
Dim hostEntry = Dns.GetHostEntry("hostname")
Dim addressList As New List(Of IPAddress)

For Each address In hostEntry.AddressList
If address.AddressFamily = Sockets.AddressFamily.InterNetwork Then
End If
Return lst

End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub
End Class

Now replace the “hostname” with some actual hostnames and execute the application by pressing F5. Click the button over the form you are done! Please note, this sample is restricted to fetch IPV4 addresses.