Click here to Skip to main content
15,881,139 members
Articles / Operating Systems / Windows
Article

XML in Table View

Rate me:
Please Sign up or sign in to vote.
1.50/5 (4 votes)
24 Jan 20074 min read 30.4K   330   8  
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.<o:p>

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. <o:p>

Using the Code

List of Classes and its Description

GetColumnNames – This is a Class to retrieve the Column names from XML file.<o:p>

GetValues – This is a Class to retrieve the XML data relative to the Column names from XML file.<o:p>

Common – This is a Class which provides common methods or subroutines to GetColumnNames and GetValues<o:p>

Class Common

Shared Function compare(ByVal arr() As String, ByVal val As String) As Boolean<o:p>

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.      <o:p>

Shared Function havechildnodata(ByVal element As XmlElement) As Boolean<o:p>

This function checks, whether the element have child but no data inside it.     <o:p>

Shared Sub loadxml(ByVal location As String, ByRef xmldoc As XmlDocument)<o:p>

Load the XML file and Returns the whole XML Document in xmldoc     <o:p>

Class GetColumnNames

Global Variables for the class      <o:p>

    <o:p>

    Private dt As New DataTable<o:p>

    Private xmldoc As New XmlDocument<o:p>

    ''' Array index for the placement of XML data in data table<o:p>

    Private Shared i As Integer<o:p>

    ''' Array index for PreviousParentIndex and PreviousParentName<o:p>

    Private Shared j As Integer<o:p>

    ''' location of the selected XML file<o:p>

    Private location As String<o:p>

   <o:p>

Public Function GetColumns() As DataTable<o:p>

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.<o:p>

Private Sub clearall()<o:p>

Sub routine to reset all the global variables. <o:p>

Private Sub createdatatable(ByRef dt As DataTable)<o:p>

Sub routine to Copy the Column names in the dataTable—dt, which is passed as reference.<o:p>

Private Function getxmlstructure() As String()<o:p>

This function returns array of strings which contains the Column names.        <o:p>

Private Sub getcolumnname(ByRef col() As String, ByVal element As XmlElement)<o:p>

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. <o:p>

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.<o:p>

    <o:p>

Private Sub getattcol(ByRef col() As String, ByVal element As XmlElement)<o:p>

For the given element, it copies all the attribute names in to the col() array.      <o:p>

Class GetValues

    <o:p>

Global Variables for the class      <o:p>

<o:p> 

    Private dt As New DataTable<o:p>

    Private dr As DataRow<o:p>

    Private xmldoc As New XmlDocument<o:p>

    ''' Array index for the placement of XML data in data table<o:p>

    Private Shared i As Integer<o:p>

    ''' Array index for PreviousParentIndex and PreviousParentName<o:p>

    Private Shared j As Integer<o:p>

    ''' Stack Array Contains the index of the previous parent<o:p>

    Private PreviousParentIndex(1000) As Integer<o:p>

    ''' Stack Array contains the previous parent names<o:p>

    Private PreviousParentName(1000) As String<o:p>

    ''' index of the column name where the data to be stored<o:p>

    Private index As Integer<o:p>

    ''' location of the selected XML file <o:p>

Public Sub addtodatatable(ByVal datatable As DataTable)<o:p>

Function to add the datas to datatable through datarow by calling elementstore(XMLElement)      <o:p>

Private Sub elementstore(ByVal element As XmlElement)<o:p>

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.<o:p>

Private Function findindex(ByVal str As String) As Integer<o:p>

For the given string str, it retrieves the index where the column name present in datatable.      <o:p>

Private Sub attributestore(ByVal element As XmlElement)<o:p>

For the given XMLElement element, it retrieves all the attribute values inside the given XMLElement.         <o:p>

That's it!!!!!     <o:p>

<o:p> 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
I am Vinodhkumar.VR, interested in DataBase.

Comments and Discussions

 
-- There are no messages in this forum --