Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Xml.XPath
Public Class frmPrepareXML
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btnInventory As System.Windows.Forms.Button
    Friend WithEvents btnEmployees As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnInventory = New System.Windows.Forms.Button
        Me.btnEmployees = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'btnInventory
        '
        Me.btnInventory.Location = New System.Drawing.Point(8, 8)
        Me.btnInventory.Name = "btnInventory"
        Me.btnInventory.Size = New System.Drawing.Size(144, 40)
        Me.btnInventory.TabIndex = 0
        Me.btnInventory.Text = "Stwórz XML produktów"
        '
        'btnEmployees
        '
        Me.btnEmployees.Location = New System.Drawing.Point(168, 8)
        Me.btnEmployees.Name = "btnEmployees"
        Me.btnEmployees.Size = New System.Drawing.Size(144, 40)
        Me.btnEmployees.TabIndex = 1
        Me.btnEmployees.Text = "Stwórz XML pracowników"
        '
        'frmPrepareXML
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(320, 61)
        Me.Controls.Add(Me.btnEmployees)
        Me.Controls.Add(Me.btnInventory)
        Me.Name = "frmPrepareXML"
        Me.Text = "ADO.NET i XML"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Dim cn As New SqlConnection("data source=localhost;initial catalog=Nowinki;user id=sa")

    Private Sub btnInventory_Click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles btnInventory.Click
        Dim dsInventory As New DataSet
        Dim daInventory As New SqlDataAdapter _
                ("select * from tblProdukty", cn)

        daInventory.Fill(dsInventory, "tblProdukty")

        ' Zapisz kolumnę ID w postaci atrybutu XML (nie w postaci osobnego elementu)
        dsInventory.Tables("tblProdukty").Columns("ID").ColumnMapping = _
                        MappingType.Attribute

        ' Ukryj w zapisywanym dokumencie XML kolumnę CenaHurtowa
        dsInventory.Tables("tblProdukty").Columns("CenaHurtowa").ColumnMapping _
                        = MappingType.Hidden

        ' Zapisz dane w postaci pliku XML (włącznie z wbudowanym schematem)
        dsInventory.WriteXml("..\Inventory.xml", XmlWriteMode.WriteSchema)

    End Sub

    Private Sub btnEmployees_Click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles btnEmployees.Click
        Dim dsEmployees As New DataSet
        Dim daEmployees As New SqlDataAdapter _
                    ("select * from tblPracownicy", cn)
        Dim daDepartments As New SqlDataAdapter _
                    ("select * from tblDzialy", cn)

        daDepartments.Fill(dsEmployees, "tblDzialy")
        daEmployees.Fill(dsEmployees, "tblPracownicy")

        ' Zdefiniuj relację pomiędzy tabelami
        dsEmployees.Relations.Add("PracownicyDzialu", _
                    dsEmployees.Tables("tblDzialy").Columns("ID"), _
                    dsEmployees.Tables("tblPracownicy").Columns("IDDzialu"))

        ' Określ węzeł potomny, który zostanie zagnieżdżony w ramach węzłów macierzystych
        dsEmployees.Relations("PracownicyDzialu").Nested = True

        ' Zapisz dane w postaci dokumentu XML
        dsEmployees.WriteXml("..\Employees.xml")

    End Sub


End Class
