65.9K
CodeProject is changing. Read more.
Home

XML in Table View

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.50/5 (4 votes)

Jan 25, 2007

4 min read

viewsIcon

30652

downloadIcon

330

View the XML in Table format using Data Grid.

Introduction

What is XML?

                XML was designed to describe data and to focus on what data is.

In this Article, I Use DOM (Document Object Model - a standard way of accessing and manipulating XML Documents) to retrieve the XML Data and displays it in a table view using Data Grid. Further, I am also using recursion to get the XML Data.

Limitations

The Code does not display the XML File correctly, when it contains the same name for element and attributes or same name for attributes which are under the different elements.

Using the Code

List of Classes and its Description

GetColumnNames – This is a Class to retrieve the Column names from XML file.

GetValues – This is a Class to retrieve the XML data relative to the Column names from XML file.

Common – This is a Class which provides common methods or subroutines to GetColumnNames and GetValues

Class Common

Shared Function compare(ByVal arr() As String, ByVal val As String) As Boolean

Searches the Value val in the Array arr(). If value is found, it returns true. Otherwise, it returns false. This function was written to avoid the duplicate column name to be added in the datatable.     

Shared Function havechildnodata(ByVal element As XmlElement) As Boolean

This function checks, whether the element have child but no data inside it.    

Shared Sub loadxml(ByVal location As String, ByRef xmldoc As XmlDocument)

Load the XML file and Returns the whole XML Document in xmldoc    

Class GetColumnNames

Global Variables for the class     

   

    Private dt As New DataTable

    Private xmldoc As New XmlDocument

    ''' Array index for the placement of XML data in data table

    Private Shared i As Integer

    ''' Array index for PreviousParentIndex and PreviousParentName

    Private Shared j As Integer

    ''' location of the selected XML file

    Private location As String

  

Public Function GetColumns() As DataTable

Function to return the column names as data table to the calling function. It uses createdatatable sub routine to get the column names in the data table.

Private Sub clearall()

Sub routine to reset all the global variables.

Private Sub createdatatable(ByRef dt As DataTable)

Sub routine to Copy the Column names in the dataTable—dt, which is passed as reference.

Private Function getxmlstructure() As String()

This function returns array of strings which contains the Column names.       

Private Sub getcolumnname(ByRef col() As String, ByVal element As XmlElement)

The most tricky Sub routine is getcolumnname(). By using this function, I want to retrieve all the column names from XML file. Here, attributes also considered as a single column.

I want to retrieve the column names from the XML files of any kind. So, we are not able to travel the XML file by tag name or attribute name. So, I had used recursion to traverse the whole XML file from top to bottom and get the entire element and attribute names in a order it traverses. The sub routine eliminates the duplicate element and attributes names by using the function compare() in class Common. This subroutine quits, When the XML file contains no sibling at last.

   

Private Sub getattcol(ByRef col() As String, ByVal element As XmlElement)

For the given element, it copies all the attribute names in to the col() array.      

Class GetValues

   

Global Variables for the class     

 

    Private dt As New DataTable

    Private dr As DataRow

    Private xmldoc As New XmlDocument

    ''' Array index for the placement of XML data in data table

    Private Shared i As Integer

    ''' Array index for PreviousParentIndex and PreviousParentName

    Private Shared j As Integer

    ''' Stack Array Contains the index of the previous parent

    Private PreviousParentIndex(1000) As Integer

    ''' Stack Array contains the previous parent names

    Private PreviousParentName(1000) As String

    ''' index of the column name where the data to be stored

    Private index As Integer

    ''' location of the selected XML file

Public Sub addtodatatable(ByVal datatable As DataTable)

Function to add the datas to datatable through datarow by calling elementstore(XMLElement)     

Private Sub elementstore(ByVal element As XmlElement)

This is to store the XML data relative to their column name. The elementstore() function works same as the getcolumnnames() in class GetColumnNames. Here, it retrieves the Values inside the element or attributes instead of names of the elment or attributes.

Private Function findindex(ByVal str As String) As Integer

For the given string str, it retrieves the index where the column name present in datatable.      

Private Sub attributestore(ByVal element As XmlElement)

For the given XMLElement element, it retrieves all the attribute values inside the given XMLElement.         

That's it!!!!!