ARTICLE

How to write DataSet in XML

Posted by Munesh Sharma Articles | ADO.NET in VB.NET April 07, 2011
In this article we will learn ,how to create Datset in xml using VB.NET.
Download Files:
 
Reader Level:


Who to Create the DataSet in XML:

Internally DataSet is represented by xml. this enables a data set to accurately mirror data returned by any data source. in fact this is one of the reasons the dataset object in ADO.NET is so much most powerful than the RecordSet object in ADo.NET.

Not only does the DataSet store data, but it also store data about the schema that was used to store the data, because DataSet is Essentially xml entities. That is used moving  data from a DataSet to XML and XML to DataSet.

Steps to create dataset in XML:

  • Open Visual Studio>new Project>window Application.

  • Design window form .

  • Coding for window form:

    Imports System.Xml
    Imports
    System.Data  
    Public
    Class Form1
        Dim ds As New DataSet
        Dim dt As DataTable
        Dim dt1 As DataTable   
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
     
                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, "Note Book Pc", 1566)
                fillRows(2, "Laptop", 26000)
                fillRows(3, "Pc Computer", 13000)
                fillRows(4, "Printer", 3999)
                ds.Tables.Add(dt)
                ds.Tables(0).TableName = "product"
                ds.WriteXml("Product1.xml")
                'Another xml file
                
                MsgBox("First Product1.xml file fill to dataset"
            Catch ex As Exception 
            End Try
     
        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
     
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
            Dim xmlFile As XmlReader
            xmlFile = XmlReader.Create("Product1.xml", New XmlReaderSettings())
            Dim ds As New DataSet
            ds.ReadXml(xmlFile)
            DataGridView1.DataSource = ds.Tables(0)    
        End Sub
     
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            dt1 = New DataTable()
            dt1.Columns.Add(New DataColumn("Product_ID", Type.GetType("System.Int32")))
            dt1.Columns.Add(New DataColumn("Product_Name", Type.GetType("System.String")))
            dt1.Columns.Add(New DataColumn("product_Price", Type.GetType("System.Int32")))
            fillRows(1, "Lux Shop", 15)
            fillRows(2, "Hamam Shop", 26)
            fillRows(3, "Detol Shop", 13)
            fillRows(4, "Baby Johnsan", 39)
            ds.Tables.Add(dt1)
            ds.Tables(1).TableName = "product2"
            ds.WriteXml("Product2.xml")
            MsgBox("Second file Product2.xml fill to dataset")
        End Sub
     
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click 
            Dim xmlFile As XmlReader
            xmlFile = XmlReader.Create("Product2.xml", New XmlReaderSettings())
            Dim ds As New DataSet
            ds.ReadXml(xmlFile)
            DataGridView2.DataSource = ds.Tables(0) 
        End Sub

    End
    Class  

     

  • Output:

    output1.gif

    output2.gif

    Output3.gif

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor