VB.Net - XML To DataGridView

This code snippet shows how to load data from an XML file into a DataGridView control using the DataSet class.
  1. // Contact.xml
  2.  
  3. <?xml version="1.0" encoding="utf-8" ?>
  4. <contacts>
  5.         <contact>
  6.                 <firstname>John</firstname>
  7.                 <lastname>Smith</lastname>
  8.                 <email>john.smith@xyzmail.com</email>
  9.         </contact>
  10.         <contact>
  11.                 <firstname>Suzan</firstname>
  12.                 <lastname>Carter</lastname>
  13.                 <email>suzan.carter@xyzmail.com</email>
  14.         </contact>
  15.         <contact>
  16.                 <firstname>Tom</firstname>
  17.                 <lastname>Baker</lastname>
  18.                 <email>tom.baker@xyzmail.com</email>
  19.         </contact>
  20. </contacts>
  21.  
  22.  
  23. Imports System.Data
  24.  
  25. Public Class Form1
  26.  
  27.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  28.  
  29.         Dim ContactsSet As New DataSet("Contacts")
  30.         ContactsSet.ReadXml("Contact.xml")
  31.         DataGridView1.DataSource = ContactsSet
  32.         DataGridView1.DataMember = "Contact"
  33.  
  34.     End Sub
  35. End Class

No comments:

Post a Comment