ARTICLE

How to create XML database in WPF using VB.NET

Posted by Rohatash Kumar Articles | WPF using VB.NET January 28, 2011
This article shows how to create database in XML format using datatable, dataset and datarow classes in Visual Studio 2010.
Download Files:
 
Reader Level:

This article shows how to create database in XML format using datatable, dataset and datarow classes in Visual Studio 2010.

Insert the data in xml file

Use DataSet class to read and write the data on disc.

  1. WriteXml(string filename)

  2. ReadXml(string filename)

The dataset is a collection data table type object. It manages Tables collections.

 

DataTable - represents a table in XML format in memory. It manages data using Rows and Columns collections.

 

Open the visual studio and drag one button named (save) three textbox and three TextBlock controls on the form. Form looks like this.


x1.gif 

Figure 1.

 

XAML code

 

<Window x:Class="MainWindow"

 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

   Title="MainWindow" Height="350" Width="525">

    <Grid>

        <TextBlock Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="TextBlock1" Text="Customerid" VerticalAlignment="Top" />

         <TextBlock Height="23" HorizontalAlignment="Left" Margin="10,43,0,0" Name="TextBlock2" Text="Name" VerticalAlignment="Top" />

          <TextBlock Height="23" HorizontalAlignment="Left" Margin="10,76,0,0" Name="TextBlock3" Text="Email" VerticalAlignment="Top" />

           <TextBox Height="23" HorizontalAlignment="Left" Margin="82,7,0,0" Name="txtCid" VerticalAlignment="Top" Width="120" />

            <TextBox Height="23" HorizontalAlignment="Left" Margin="82,43,0,0" Name="txtName" VerticalAlignment="Top" Width="120" />

             <TextBox Height="23" HorizontalAlignment="Left" Margin="82,76,0,0" Name="txtEmail" VerticalAlignment="Top" Width="120" />

               <Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="82,134,0,0" Name="Button1" VerticalAlignment="Top" Width="75" />

                 </Grid>

                    </Window>

 

Now add the following VB code.

 

Imports System.Collections.Generic

Imports System.ComponentModel

Imports System.Data

Imports System.Linq

Imports System.Text

Imports System.IO

Imports System.Xml

 

Class MainWindow

  Private dt As DataTable

   Private ds As DataSet

   

    Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded

        ds = New DataSet()

        dt = New DataTable("Customer")

        dt.Columns.Add("Cid")

        dt.Columns.Add("Name")

        dt.Columns.Add("Email")

        ds.Tables.Add(dt)

        End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click

        Dim dr As DataRow = dt.NewRow()

        dr(0) = txtCid.Text

        dr(1) = txtName.Text

        dt.Rows.Add(dr)

        dr(2) = txtEmail.Text

        ds.WriteXml("E:/customer4.xml")

        MessageBox.Show(" Data has been saved")

        txtCid.Clear()

        txtEmail.Clear()

        txtName.Clear()

    End Sub

End Class

 

Now run the application and enter the customer id value, name and email id and click on the Button save.

x2.gif

Figure 2.

Now click on the OK and record has been saved in the XML file and XML file looks like this.

      <?xml version="1.0" standalone="yes" ?>

-     <NewDataSet>

-     <Customer>

      <Cid>3</Cid>

     <Name>Rahul</Name>

     <Email>rahul.20@gmail.com</Email>

  </Customer>

  </NewDataSet>

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor