Quantcast
Viewing latest article 4
Browse Latest Browse All 5

How to load XML to Datagridview

Loading xml from file to datagridview

  • Requirements: Visual Studio 2005, 2008, 2010
  • Programming Level: Moderate
  • Language: Visual Basic.Net

Scenario

An XML is stands for Extensible Markup Language, that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable [1]. In a modern programming architecture xml plays an important role especially on data transportation in which we can use it to transport data from system to system. In this article I am going to explain and give some useful examples to demonstrate the capability of an XML to import and export data.

How it will be done?

You will gonna need to add three controls on your form, OpenFileDialog, button and Datagridview, The OpenFileDialog is use to select the required xml File on your machine and the Datagridview is use to populate data from an xml file. First you need add a new windows form and add the Button, OpenFileDialog and the Datagridview on to the form, take note that the OpenFileDialog will not appear on the form as it has no visible properties at runtime. Configure the OpenFileDialog control from the Properties dialog box and set the ff: Filename = , Filter=XML Fle (*.xml)|*.xml, Title=Open XML File, Add Columns to datagridview to the ff: columns: GridBookID, GridBookName, GridBookType, GridCategory respectively as shown below:

Sample Windows Forms

The Source Code

#Region "XML"
     Public Shared Function EmptyStringToNull(o As String) As Object
        Dim ret As Object = DBNull.Value
        Try
            If o.Trim.Length = 0 Then
                ret = DBNull.Value
            Else
                ret = o
            End If
        Catch ex As Exception

        End Try
        Return ret
    End Function
    Private Sub ReadXMLFile()
        Dim xmlDoc As New System.Xml.XmlDocument
        Dim root As XmlElement = Nothing
        Dim nodes As XmlNodeList = Nothing
        Dim node As XmlNode = Nothing
        Dim xmlFile As String = ""
        Try
            OpenFileDialog1.ShowDialog()
            xmlFile = OpenFileDialog1.FileName
            xmlDoc.Load(xmlFile)
            root = xmlDoc.DocumentElement
            nodes = root.SelectNodes("//XMLDocument/Books/Book") 'The XMLPath
            Me.dtgBook.Rows.Clear() 'Clear Grid
            For Each node In nodes
                Me.dtgBook.Rows.Add(EmptyStringToNull(node("BookID").InnerText), EmptyStringToNull(node("BookName").InnerText), EmptyStringToNull(node("BookType").InnerText), EmptyStringToNull(node("Category").InnerText))
            Next
        Catch ex As Exception

        End Try
    End Sub
#End Region
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        ReadXMLFile()
    End Sub
You need an XML File to load it to datagridview, below is the xml file named “XMLBook.xml” save it to your drive like in c: the content of an xml is given below.
<?xml version="1.0" encoding="utf-8" ?>
<XMLDocument>
  <Books>
    <Book>
      <BookID>1</BookID>
      <BookName>Programming c++</BookName>
      <BookType>Programming</BookType>
      <Category>Education</Category>
    </Book>
    <Book>
      <BookID>2</BookID>
      <BookName>Programming c#</BookName>
      <BookType>Programming</BookType>
      <Category />
    </Book>
    <Book>
      <BookID>3</BookID>
      <BookName>VB.net</BookName>
      <BookType>Programming</BookType>
      <Category>Education</Category>
    </Book>
    <Book>
      <BookID>4</BookID>
      <BookName>Cobol</BookName>
      <BookType>Programming</BookType>
      <Category>Education</Category>
    </Book>
  </Books>
</XMLDocument>
The Above xml will be use to load it to datagridview using the XmlDocument class, you just need to add references on System.Xml, place the ff: imports statement above Public Class declarations
Imports System.Xml

Keywords: , , , , , , , , , , , , , ,

Other reading this article are also reading these:


Viewing latest article 4
Browse Latest Browse All 5

Trending Articles