Friday, October 2, 2009

Manipulating XML Data Using DataSet Class

This article describes how to use a windows form in VB.NET to manage data in XML files. The code snippets in this article demonstrate inserting, updating, and deleting row entries from an XML data file that has been loaded into the DataSet class using the ReadXML method. This article also demonstrates how to display the data from the XML file.

I Use DataGridView to list the XML File and each row of DataGridView contains two buttons EDIT and DELETE for the purpose of updating and deleting XML File

Sample XML File


For the examples in this article, I use the TestXML.xml.


<?xml version="1.0" standalone="yes" ?>

-
<NODES>

-
<Details>


<ID>1</ID>


<Name>Mr.XX</Name>


<Age>28</Age>


<DOJ>12/29/2006</DOJ>


</Details>

-
<Details>


<ID>2</ID>


<Name>MR.YY</Name>


<Age>27</Age>


<DOJ>9/16/2009</DOJ>


</Details>

-
<Details>


<ID>3</ID>


<Name>Ram</Name>


<Age>29</Age>


<DOJ>10/2/2009</DOJ>


</Details>


</NODES>


You'll need the following namespaces in the VB.NET code for execution:

Imports System.Xml

Reading XML into a DataSet

.NET provides the DataSet class with methods that read and parse an XML file for access within code.

The following code snippet illustrates the XMLRead and XMLWrite methods from the DataSet class

Dim ds As New DataSet

Try

ds.ReadXml("C:\Documents\TestXML.xml")

ds.WriteXml("C:\Documents\TestXML.xml")


Catch ex As Exception

MsgBox(ex.Message)


End Try


The DataSet class contains an array of rows, each row called Details with Four columns called ID , Name, Age and DOJ.

Listing Data

If you want to list the contents of the DataSet in the same order as the XML file, use the following code:


Try

ds.ReadXml("C:\Viji\Documents\TestXML.xml")


Catch ex As Exception

MsgBox(ex.Message)


Exit Sub


End

Try


DataGridView1.Columns.Clear()

DataGridView1.DataSource = Nothing



If Not ds Is Nothing Then

DataGridView1.DataSource = ds.Tables(0)


End If



If DataGridView1.Rows.Count > 0 Then


Dim dgButtoncol As
New DataGridViewButtonColumn

dgButtoncol.UseColumnTextForButtonValue = True


dgButtoncol.HeaderText = "View"

dgButtoncol.Text = "View"

dgButtoncol.Name = "View"

DataGridView1.Columns.Add(dgButtoncol)


dgButtoncol = New DataGridViewButtonColumn

dgButtoncol.UseColumnTextForButtonValue = True

dgButtoncol.HeaderText = "Edit"

dgButtoncol.Text = "Edit"

dgButtoncol.Name = "Edit"

DataGridView1.Columns.Add(dgButtoncol)



dgButtoncol = New DataGridViewButtonColumn

dgButtoncol.UseColumnTextForButtonValue = True

dgButtoncol.HeaderText = "Delete"

dgButtoncol.Text = "Delete"

dgButtoncol.Name = "Delete"

DataGridView1.Columns.Add(dgButtoncol)

End If


Inserting Data

The following code sample shows how to insert a new row into the DataSet:


Try


Dim ID As String


ID = ds.Tables(0).Rows.Count + 1



Dim dr As DataRow = ds.Tables(0).NewRow

dr("ID") = ID

dr("Name") = txtName.Text.Trim

dr("Age") = txtAge.Text.Trim

dr("DOJ") = DateTimePicker1.Value.ToShortDateString


ds.Tables(0).Rows.Add(dr)


ds.WriteXml("C:\Documents\TestXML.xml")


Catch ex As Exception

MsgBox(ex.Message)


End Try


Where txtName, txtAge (text boxes) and DateTimePicker1 controls are used to let the user enter new value.


Updating Data

To update a row in the DataSet, you must locate the row and update the fields within the row. A key field must be selected and searched, using the value of the field prior to the change, to locate the correct row for update. For this example the "ID" field is used as the key field.

If the Name of the ID 1, MR.XX should have been Mr.XXX, here is an example of how you could change it:


Try


If Not ds Is Nothing Then


For
Each dr As DataRow In ds.Tables(0).Rows


Dim ID As String = Convert.ToString(dr("ID"))



If ID = txtID.Text Then


dr("Name") = txtName.Text.Trim

dr("Age") = txtAge.Text.Trim

dr("DOJ") = DateTimePicker1.Value.ToShortDateString


Exit For


End If


Next


ds.WriteXml("C:\Documents\TestXML.xml")

End If

Catch ex As Exception

MsgBox(ex.Message)


End Try


Deleting Data

To delete the Mr.XX Data , a key field must be selected and searched, using the value of the field prior to the delete, to locate the correct row for removal. For this example the "ID" field is used again as the key field. Use the following code:

'I clicked on DataGridView Delete Button

Dim ID As String = sender.Rows(e.RowIndex).Cells("ID").Value.ToString.Trim



For Each dr As DataRow In ds.Tables(0).Rows


Dim rowID As String = Convert.ToString(dr("ID"))


If ID = rowID Then

dr.Delete()


Exit For


End If


Next

ds.WriteXml("C:\Viji\Documents\TestXML.xml")


This article has explained the concepts of editing, updating and deleting records from XML File using Dataset.




No comments:

Post a Comment