วันพุธที่ 12 กุมภาพันธ์ พ.ศ. 2557

การอ่าน XML ลง Datatable โดยใช้ vb.net

การอ่าน XML ลง DataTable โดยใช้ vb.net


1 สร้าง windows form application 2 เลือก DataGridView  จาก tool `ด้านซ้ายมือ


3 add event form load โดยการ double click ที่พื้นที่ว่างเปล่า


ใส่ code ดังนี้

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim ds As DataSet
        ds = New DataSet
        ds.ReadXml("a.xml")
        Me.DataGridView1.DataSource = ds.Tables(0)
    End Sub
End Class



โดยที่ให้อ่าน file XML ที่อยู่ที่ default path (โดยปกติ คือ ที่เดียกกับ file .exe  (../debug/))

เนื้อหาของ file xml มีดังนี้

 
<customers>
<customer>
<name>a</name>
<lastname>a</lastname>
<phone>a</phone>
</customer>
<customer>
<name>b</name>
<lastname>b</lastname>
<phone>b</phone>
</customer>
<customer>
<name>c</name>
<lastname>c</lastname>
<phone>c</phone>
</customer>
<customer>
<name>d</name>
<lastname>d</lastname>
<phone>d</phone>
</customer>
</customers>

ผลลัพธ์ที่ได้



สอนเขียนโปรแกรม vb.net
http://www.expert-programming-tutor.com/




ไม่มีความคิดเห็น:

แสดงความคิดเห็น