Using XML in .NET August 2, 2008
Posted by tuse in : ASP.NET, Tools, VB.NET , trackbackWe will see how we can use XML files in a VB.NET application. XML is used in many aspects of web development, often to simplify data storage.
In a Windows form, add a DataGridView and a Button. Change the text of this Button as ‘Save’.
The XML file that I am interested in-
John Smith
Computers
Matthew Max
Electrical
Jay Yan
Mechanical
The XML file that I have holds freshman records studying in an engineering school. We will look to display the fields ‘name’ and ‘branch’ in the DataGridView.
The ‘Save’ button should be able to save the changes made to the records in the DataGridView in the XML file.
We shall use a DataSet for reading the XML file. The Save Button will involve the Write function to the XML file invoked using the dataset.
The code-
Public Class Form1
Dim ds As DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ds = New DataSet
ds.ReadXml("C:\myxml\file1.xml") 'Path where your XML file exists on the system
Me.DataGridView1.DataSource = ds
Me.DataGridView1.DataMember = "freshman" 'Records that you wish to display. Look at the XML file for reference
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ds.WriteXml("C:\myxml\file1.xml") 'Save the changes made in the DataGridView to be reflected in the XML file
End Sub
End Class
This is an illustration of the usage of XML in VB.NET. You may use this in ASP.NET too. In order to specify the source for the file on the server, you may use ‘Server.MapPath()’ to specify the path for your XML file.

Comments»
[...] News » News News Using XML in .NET2008-08-06 07:09:43Xml an engineering school. We will look to display the DataGridView. … have [...]
Hi
I am completely new to VB, the article that was posted is exactly what I am looking and works a treat with my project, as I am working with XML data.
I was just wondering, how this code can be updated to move Items from Textboxes, then into DGV Control, and then append to my XML file.
Any help would be appreciated