Monday, February 20, 2012

VB.NET Write data to XML using a DataSet


In this tutorial i will talk about getting the name of all the files (in this case images) in a directory and store them into a XML file using a DataSet in VB.NET.


Code:
Imports System.IO

Public Class Form1
    Dim images_dir As String = "C:\myfolder"
    Dim xml_dir As String = "C:\myfolder"
    Dim image_list As String()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataSet_images.Tables("Table1").Clear()
        image_list = Directory.GetFiles(images_dir)
        Dim filename As String
        Dim counter As Integer
        For Each filename In image_list
            counter += 1
            DataSet_images.Tables("Table1").Rows.Add(New Object() {counter, filename})
        Next filename
        DataSet_images.Tables("Table1").WriteXml(xml_dir + "\images.xml", XmlWriteMode.IgnoreSchema)
    End Sub
End Class
Video Tutorial:





No comments:

Post a Comment