Parse a xml file for multiple rows and columns using XDocument and VB.net

Rajnilari2015
Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 320
Imports System.Collections.Generic
Imports System.Xml.Linq
Namespace ConsoleApplication1
Class Program
Private Shared Sub Main(args As String())
Dim doc As XDocument = XDocument.Load("test.xml")
Dim tblCollection = New List(Of Table)()
For Each tableElement As var In doc.Descendants("table")
Dim tbl = New Table()
tbl.TblName = tableElement.Attribute("table_name").Value
'set the table name
Dim tblColHeadings = New TableColHeadings()
'loop for the table_col_headings
For Each tableCOLElement As var In tableElement.Descendants("table_col_headings")
tblColHeadings.ColHeading4 = tableCOLElement.Attribute("col_heading_4").Value
tblColHeadings.ColHeading5 = tableCOLElement.Attribute("col_heading_5").Value
tblColHeadings.ColHeading6 = tableCOLElement.Attribute("col_heading_6").Value
tblColHeadings.ColHeading7 = tableCOLElement.Attribute("col_heading_7").Value
tblColHeadings.ColHeading8 = tableCOLElement.Attribute("col_heading_8").Value
'set the column headings
tbl.TblColHeadings = tblColHeadings
Next
Dim tblRows = New TableRows()
tblRows.TblRow = New List(Of TableRow)()
'loop for the table_row
For Each tableROWData As var In tableElement.Descendants("table_row")
Dim tblRow = New TableRow()
tblRow.row_caption = tableROWData.Attribute("row_caption").Value
tblRow.col_data4 = tableROWData.Attribute("col_data4").Value
tblRow.col_data5 = tableROWData.Attribute("col_data5").Value
tblRow.col_data6 = tableROWData.Attribute("col_data6").Value
tblRow.col_data7 = tableROWData.Attribute("col_data7").Value
tblRow.col_data8 = tableROWData.Attribute("col_data8").Value
tblRows.TblRow.Add(tblRow)
Next
tbl.TblRows = tblRows
'set the table Rows
'add to the Table collection
tblCollection.Add(tbl)
Next
End Sub
End Class

Public Class TableRow
Public Property row_caption() As String
Get
Return m_row_caption
End Get
Set
m_row_caption = Value
End Set
End Property
Private m_row_caption As String
Public Property col_data4() As String
Get
Return m_col_data4
End Get
Set
m_col_data4 = Value
End Set
End Property
Private m_col_data4 As String
Public Property col_data5() As String
Get
Return m_col_data5
End Get
Set
m_col_data5 = Value
End Set
End Property
Private m_col_data5 As String
Public Property col_data6() As String
Get
Return m_col_data6
End Get
Set
m_col_data6 = Value
End Set
End Property
Private m_col_data6 As String
Public Property col_data7() As String
Get
Return m_col_data7
End Get
Set
m_col_data7 = Value
End Set
End Property
Private m_col_data7 As String
Public Property col_data8() As String
Get
Return m_col_data8
End Get
Set
m_col_data8 = Value
End Set
End Property
Private m_col_data8 As String
End Class

Public Class TableColHeadings
Public Property ColHeading4() As String
Get
Return m_ColHeading4
End Get
Set
m_ColHeading4 = Value
End Set
End Property
Private m_ColHeading4 As String
Public Property ColHeading5() As String
Get
Return m_ColHeading5
End Get
Set
m_ColHeading5 = Value
End Set
End Property
Private m_ColHeading5 As String
Public Property ColHeading6() As String
Get
Return m_ColHeading6
End Get
Set
m_ColHeading6 = Value
End Set
End Property
Private m_ColHeading6 As String
Public Property ColHeading7() As String
Get
Return m_ColHeading7
End Get
Set
m_ColHeading7 = Value
End Set
End Property
Private m_ColHeading7 As String
Public Property ColHeading8() As String
Get
Return m_ColHeading8
End Get
Set
m_ColHeading8 = Value
End Set
End Property
Private m_ColHeading8 As String
End Class

Public Class TableRows
Public Property TblRow() As List(Of TableRow)
Get
Return m_TblRow
End Get
Set
m_TblRow = Value
End Set
End Property
Private m_TblRow As List(Of TableRow)
End Class

Public Class Table
Public Property TblName() As String
Get
Return m_TblName
End Get
Set
m_TblName = Value
End Set
End Property
Private m_TblName As String
'store table name
Public Property TblColHeadings() As TableColHeadings
Get
Return m_TblColHeadings
End Get
Set
m_TblColHeadings = Value
End Set
End Property
Private m_TblColHeadings As TableColHeadings
'store column headings info
Public Property TblRows() As TableRows
Get
Return m_TblRows
End Get
Set
m_TblRows = Value
End Set
End Property
Private m_TblRows As TableRows
'stores table rows info
End Class
End Namespace

Comments or Responses

Login to post response