S
Shan1986
Guest
Hallo,
I am trying to convert data table to class objects . I have a following code which works for commented data table but it does not work for the data table created from the XML import why is that?
XML
<?xml version="1.0" encoding="UTF-8"?>
-<dataroot generated="2020-04-20T16:36:48" xmlnsd="urn:schemas-microsoft-comfficedata">
-<Tb1>
<Name>Name1</Name>
<Address>Address1</Address>
<City>City1</City>
<Zip>Zip1</Zip>
</Tb1>
-<Tb1>
<Name>Name2</Name>
<Address>Address2</Address>
<City>City2</City>
<Zip>Zip2</Zip>
</Tb1>
-<Tb1>
<Name>Name3</Name>
<Address>Address3</Address>
<City>City3</City>
<Zip>Zip3</Zip>
</Tb1>
-<Tb1>
<Name>Name4</Name>
<Address>Address4</Address>
<City>City4</City>
<Zip>Zip4</Zip>
</Tb1>
</dataroot>
Imports System.IO
Imports System.Reflection
Public Class Form1
Public XML_DS As New DataSet
Public DS As New DataSet
Public dt As New DataTable
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call ImportXMLs()
dt = XML_DS.Tables("Tb1")
'dt.Columns.Add(New DataColumn("Name"))
'dt.Columns.Add(New DataColumn("Address"))
'dt.Columns.Add(New DataColumn("City"))
'dt.Columns.Add(New DataColumn("Zipcode"))
'dt.Rows.Add("Name1", "Address1", "City1", "Zipcode1")
'dt.Rows.Add("Name2", "Address2", "City2", "Zipcode2")
'dt.Rows.Add("Name3", "Address3", "City3", "Zipcode3")
'dt.Rows.Add("Name4", "Address4", "City4", "Zipcode4")
End Sub
Public Class Equipment
Public Property Name As String
Public Property Address As String
Public Property City As String
Public Property Zipcode As Integer
End Class
Public Shared Function ConvertDT2List(Of T)(tabel As DataTable) As IList(Of T)
If tabel Is Nothing Then
Return Nothing
End If
Dim lines As New List(Of DataRow)()
For Each row As DataRow In tabel.Rows
lines.Add(row)
Next
Return ConvertDataRow2List(Of T)(lines)
End Function
Public Shared Function ConvertDataRow2List(Of T)(lines As IList(Of DataRow)) As IList(Of T)
Dim list As IList(Of T) = Nothing
If lines IsNot Nothing Then
list = New List(Of T)()
For Each line As DataRow In lines
Dim item As T = CreateItem(Of T)(line)
list.Add(item)
Next
End If
Return list
End Function
Public Shared Function CreateItem(Of T)(row As DataRow) As T
Dim ColName As String
Dim [object] As T = Nothing
If row IsNot Nothing Then
[object] = Activator.CreateInstance(Of T)()
For Each column As DataColumn In row.Table.Columns
ColName = column.ColumnName
Dim prop As PropertyInfo = [object].[GetType]().GetProperty(ColName)
Try
Dim value As Object = If((row(ColName).[GetType]() = GetType(DBNull)), Nothing, row(ColName))
prop.SetValue([object], value, Nothing)
Catch
Throw
End Try
Next
End If
Return [object]
End Function
Sub ImportXMLs()
Dim XMLPath As ArrayList = New ArrayList
Dim root As String = "C:\Temp\xml" '//Folder Name in Project folder
For Each filePath As String In Directory.EnumerateFiles(root)
XMLPath.Add(filePath.ToString)
Next
If XMLPath.Count > 0 Then
Dim tempDS() As DataSet = New DataSet((XMLPath.Count) - 1) {}
Dim impFiles As Integer = 0
For Each ob As Object In XMLPath
Dim impDS As DataSet = New DataSet
impDS.ReadXml(ob.ToString)
tempDS(impFiles) = impDS
impFiles = (impFiles + 1)
Next
For Each DS As DataSet In tempDS
For Each table As DataTable In DS.Tables
XML_DS.Merge(table)
Next
Next
XMLPath = Nothing
tempDS = Nothing
Else
MessageBox.Show("No XML files were found!")
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim EQlist As IList(Of Equipment) = ConvertDT2List(Of Equipment)(dt)
DataGridView1.DataSource = EQlist
End Sub
End Class
or any other simple method to do?
Continue reading...
I am trying to convert data table to class objects . I have a following code which works for commented data table but it does not work for the data table created from the XML import why is that?
XML
<?xml version="1.0" encoding="UTF-8"?>
-<dataroot generated="2020-04-20T16:36:48" xmlnsd="urn:schemas-microsoft-comfficedata">
-<Tb1>
<Name>Name1</Name>
<Address>Address1</Address>
<City>City1</City>
<Zip>Zip1</Zip>
</Tb1>
-<Tb1>
<Name>Name2</Name>
<Address>Address2</Address>
<City>City2</City>
<Zip>Zip2</Zip>
</Tb1>
-<Tb1>
<Name>Name3</Name>
<Address>Address3</Address>
<City>City3</City>
<Zip>Zip3</Zip>
</Tb1>
-<Tb1>
<Name>Name4</Name>
<Address>Address4</Address>
<City>City4</City>
<Zip>Zip4</Zip>
</Tb1>
</dataroot>
Imports System.IO
Imports System.Reflection
Public Class Form1
Public XML_DS As New DataSet
Public DS As New DataSet
Public dt As New DataTable
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call ImportXMLs()
dt = XML_DS.Tables("Tb1")
'dt.Columns.Add(New DataColumn("Name"))
'dt.Columns.Add(New DataColumn("Address"))
'dt.Columns.Add(New DataColumn("City"))
'dt.Columns.Add(New DataColumn("Zipcode"))
'dt.Rows.Add("Name1", "Address1", "City1", "Zipcode1")
'dt.Rows.Add("Name2", "Address2", "City2", "Zipcode2")
'dt.Rows.Add("Name3", "Address3", "City3", "Zipcode3")
'dt.Rows.Add("Name4", "Address4", "City4", "Zipcode4")
End Sub
Public Class Equipment
Public Property Name As String
Public Property Address As String
Public Property City As String
Public Property Zipcode As Integer
End Class
Public Shared Function ConvertDT2List(Of T)(tabel As DataTable) As IList(Of T)
If tabel Is Nothing Then
Return Nothing
End If
Dim lines As New List(Of DataRow)()
For Each row As DataRow In tabel.Rows
lines.Add(row)
Next
Return ConvertDataRow2List(Of T)(lines)
End Function
Public Shared Function ConvertDataRow2List(Of T)(lines As IList(Of DataRow)) As IList(Of T)
Dim list As IList(Of T) = Nothing
If lines IsNot Nothing Then
list = New List(Of T)()
For Each line As DataRow In lines
Dim item As T = CreateItem(Of T)(line)
list.Add(item)
Next
End If
Return list
End Function
Public Shared Function CreateItem(Of T)(row As DataRow) As T
Dim ColName As String
Dim [object] As T = Nothing
If row IsNot Nothing Then
[object] = Activator.CreateInstance(Of T)()
For Each column As DataColumn In row.Table.Columns
ColName = column.ColumnName
Dim prop As PropertyInfo = [object].[GetType]().GetProperty(ColName)
Try
Dim value As Object = If((row(ColName).[GetType]() = GetType(DBNull)), Nothing, row(ColName))
prop.SetValue([object], value, Nothing)
Catch
Throw
End Try
Next
End If
Return [object]
End Function
Sub ImportXMLs()
Dim XMLPath As ArrayList = New ArrayList
Dim root As String = "C:\Temp\xml" '//Folder Name in Project folder
For Each filePath As String In Directory.EnumerateFiles(root)
XMLPath.Add(filePath.ToString)
Next
If XMLPath.Count > 0 Then
Dim tempDS() As DataSet = New DataSet((XMLPath.Count) - 1) {}
Dim impFiles As Integer = 0
For Each ob As Object In XMLPath
Dim impDS As DataSet = New DataSet
impDS.ReadXml(ob.ToString)
tempDS(impFiles) = impDS
impFiles = (impFiles + 1)
Next
For Each DS As DataSet In tempDS
For Each table As DataTable In DS.Tables
XML_DS.Merge(table)
Next
Next
XMLPath = Nothing
tempDS = Nothing
Else
MessageBox.Show("No XML files were found!")
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim EQlist As IList(Of Equipment) = ConvertDT2List(Of Equipment)(dt)
DataGridView1.DataSource = EQlist
End Sub
End Class
or any other simple method to do?
Continue reading...