XML DataSet : DataSet XML « Database ADO.net « VB.Net






XML DataSet

  
Imports System.Data.SqlClient
Imports System.Windows.Forms
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class XmlTest
    Inherits System.Windows.Forms.Form

    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.grid = New System.Windows.Forms.DataGridView
        Me.txtFile = New System.Windows.Forms.TextBox
        Me.cmdRead = New System.Windows.Forms.Button
        Me.cmdWrite = New System.Windows.Forms.Button
        CType(Me.grid, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        Me.grid.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.grid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
        Me.grid.Location = New System.Drawing.Point(11, 43)
        Me.grid.Name = "grid"
        Me.grid.Size = New System.Drawing.Size(352, 212)
        Me.grid.TabIndex = 8
        '
        Me.txtFile.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.txtFile.Location = New System.Drawing.Point(11, 14)
        Me.txtFile.Size = New System.Drawing.Size(160, 21)
        Me.txtFile.TabIndex = 7
        Me.txtFile.Text = "c:\DataSet.xml"
        '
        Me.cmdRead.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.cmdRead.Location = New System.Drawing.Point(271, 12)
        Me.cmdRead.Size = New System.Drawing.Size(92, 24)
        Me.cmdRead.TabIndex = 6
        Me.cmdRead.Text = "Read From XML"
        '
        Me.cmdWrite.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.cmdWrite.Location = New System.Drawing.Point(183, 12)
        Me.cmdWrite.Size = New System.Drawing.Size(84, 24)
        Me.cmdWrite.TabIndex = 5
        Me.cmdWrite.Text = "Write to XML"
        '
        'XmlTest
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(375, 266)
        Me.Controls.Add(Me.grid)
        Me.Controls.Add(Me.txtFile)
        Me.Controls.Add(Me.cmdRead)
        Me.Controls.Add(Me.cmdWrite)
        Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Name = "XmlTest"
        Me.Text = "XmlTest"
        CType(Me.grid, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents grid As System.Windows.Forms.DataGridView
    Friend WithEvents txtFile As System.Windows.Forms.TextBox
    Friend WithEvents cmdRead As System.Windows.Forms.Button
    Friend WithEvents cmdWrite As System.Windows.Forms.Button

    Private Sub cmdWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdWrite.Click
        Dim SQL As String = "SELECT * FROM Products"
        Dim Connect As String = "Data Source=localhost;Integrated Security=True;Initial Catalog=Northwind;"

        Dim con As New SqlConnection(Connect)
        Dim cmd As New SqlCommand(SQL, con)
        Dim adapter As New SqlDataAdapter(cmd)

        Dim dsNorthwind As New DataSet()
        con.Open()
        adapter.Fill(dsNorthwind, "Products")
        con.Close()
        dsNorthwind.WriteXmlSchema(txtFile.Text & ".xsd")
        dsNorthwind.WriteXml(txtFile.Text)
    End Sub

    Private Sub cmdRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRead.Click
        Dim dsNorthwind As New DataSet()
        If System.IO.File.Exists(txtFile.Text) Then
            dsNorthwind.ReadXml(txtFile.Text)
            grid.DataSource = dsNorthwind.Tables(0)
        Else
            MessageBox.Show("You need to create this file first.")
        End If
    End Sub

End Class

   
    
  








Related examples in the same category

1.Write Data in DataSet to XML fileWrite Data in DataSet to XML file
2.Generating XML from an ADO.NET DataSetGenerating XML from an ADO.NET DataSet
3.Display XML String from an ADO.NET DataSetDisplay XML String from an ADO.NET DataSet