How to create an XML file in VB.NET using Dataset

By: Issac Viewed: 153208 times    

We can create XML in several ways; we need a text editor to write a XML file, it is a tag based language that means the document is made up of XML tags that contain information. 

Here we are going to create an XML file Product.XML using an ADO.NET Dataset. For that we have to manually create a Datatable first and add the data of Product.XML in the Datatable. Then add the Datatable in a Dataset. Call the method WriteXml of Dataset and pass the file name Product.XML as argument.


Imports System.Xml
Imports System.Data

Public Class Form1
    Dim dt As DataTable
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ds As New DataSet
        dt = New DataTable()
        dt.Columns.Add(New DataColumn("Product_ID", Type.GetType("System.Int32")))
        dt.Columns.Add(New DataColumn("Product_Name", Type.GetType("System.String")))
        dt.Columns.Add(New DataColumn("product_Price", Type.GetType("System.Int32")))
        fillRows(1, "product1", 1111)
        fillRows(2, "product2", 2222)
        fillRows(3, "product3", 3333)
        fillRows(4, "product4", 4444)
        ds.Tables.Add(dt)
        ds.Tables(0).TableName = "product"
        ds.WriteXml("Product.xml")
        MsgBox("Done")
    End Sub

    Private Sub fillRows(ByVal pID As Integer, ByVal pName As String, ByVal pPrice As Integer)
        Dim dr As DataRow
        dr = dt.NewRow()
        dr("Product_ID") = pID
        dr("Product_Name") = pName
        dr("product_Price") = pPrice
        dt.Rows.Add(dr)
    End Sub
End Class

Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)

Comment on this tutorial