Net-informations.com
SiteMap  | About    

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

XML is a tag based language, that means the document is made up of XML tags that contain information. We can create an XML file in several ways.

In the previous section we create an XML file using XmlTextWriter Class. Here we are creating 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




net-informations.com (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.