使用XML + XLST进行实体类代码生成 (四)
五, 生成customized dataset类
续上次的内容,在上一帖中,我们用xslt生成了需要的实体类。这次我们来看看如何来生成自定义的dataset类。
有了前面的经验接下来,生成dataset就简单多了,只需要写生成一个新的xslt模板就可以了。
续上次的内容,在上一帖中,我们用xslt生成了需要的实体类。这次我们来看看如何来生成自定义的dataset类。
有了前面的经验接下来,生成dataset就简单多了,只需要写生成一个新的xslt模板就可以了。
1
<?xml version="1.0" encoding="UTF-8" ?>
2
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
3
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
4
<xsl:template match="/Schema">Imports System.Runtime.Serialization
5
6
Namespace Data
7
<xsl:text disable-output-escaping="yes"><</xsl:text>System.ComponentModel.DesignerCategory("Code"), SerializableAttribute()<xsl:text disable-output-escaping="yes">></xsl:text> Public Class dsEntity<xsl:apply-templates select="//TableName" />
8
Inherits DataSet
9
'
10
' table constants
11
'
12
Public Const Table_Name As String = "<xsl:apply-templates select="//TableName" />"
13
'
14
' feilds constants
15
'
16
<xsl:for-each select="FIELDS/FIELD">Public Const FIELD_<xsl:value-of select="@Name" /> As String = "<xsl:value-of select="@Name" />"
17
</xsl:for-each>
18
19
'----------------------------------------------------------------
20
' Sub New:
21
' Constructor to support serialization.
22
'----------------------------------------------------------------
23
24
Private Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
25
MyBase.New(info, context)
26
End Sub
27
28
'----------------------------------------------------------------
29
' Sub New:
30
' Initialize a CustomerData instance by building the table schema.
31
'----------------------------------------------------------------
32
Public Sub New()
33
MyBase.New()
34
'
35
' Create the tables in the dataset
36
'
37
BuildDataTables()
38
End Sub
39
40
Public Sub New(ByVal i<xsl:apply-templates select="//TableName" /> As Entity<xsl:apply-templates select="//TableName" />)
41
MyBase.New()
42
43
'
44
' Create the tables in the dataset
45
'
46
BuildDataTables()
47
'
48
Add(i<xsl:apply-templates select="//TableName" />)
49
End Sub
50
51
'----------------------------------------------------------------
52
' Sub BuildDataTables:
53
' Creates the datatables
54
'----------------------------------------------------------------
55
Private Sub BuildDataTables()
56
'Create the tabe that will be returned
57
' Create the Event table
58
'
59
Dim table As DataTable = New DataTable(Table_Name)
60
61
'Create the tabe that will be returned
62
' Create the Event column
63
With table.Columns
64
<xsl:for-each select="FIELDS/FIELD"> .Add(FIELD_<xsl:value-of select="@Name" />, GetType(<xsl:value-of select="@Type" />))
65
</xsl:for-each>End With
66
67
'add the table
68
Me.Tables.Add(table)
69
End Sub
70
71
#Region "Properties"
72
Default Public Property Item(ByVal index As Integer) As Entity<xsl:apply-templates select="//TableName" />
73
Get
74
Return getclbMember(index)
75
End Get
76
Set(ByVal Value As Entity<xsl:apply-templates select="//TableName" />)
77
setclbMember(index, Value)
78
End Set
79
End Property
80
81
Private Function getclbMember(ByVal index As Integer) As Entity<xsl:apply-templates select="//TableName" />
82
Dim i<xsl:apply-templates select="//TableName" /> As New Entity<xsl:apply-templates select="//TableName" />
83
With Tables(Table_Name).Rows(index)
84
<xsl:for-each select="FIELDS/FIELD"> i<xsl:apply-templates select="//TableName" />.<xsl:value-of select="@Name" /> = .Item(FIELD_<xsl:value-of select="@Name" />)
85
</xsl:for-each>End With
86
Return i<xsl:apply-templates select="//TableName" />
87
End Function
88
89
Private Sub setclbMember(ByVal index As Integer, ByVal i<xsl:apply-templates select="//TableName" /> As Entity<xsl:apply-templates select="//TableName" />)
90
With Tables(Table_Name).Rows(index)
91
.BeginEdit()<xsl:for-each select="FIELDS/FIELD">
92
.Item(FIELD_<xsl:value-of select="@Name" />) = i<xsl:apply-templates select="//TableName" />.<xsl:value-of select="@Name" />
93
</xsl:for-each>
94
.EndEdit()
95
End With
96
End Sub
97
#End Region
98
99
Public Function Add(ByVal i<xsl:apply-templates select="//TableName" /> As Entity<xsl:apply-templates select="//TableName" />) As Integer
100
Dim dr As DataRow
101
dr = Tables(Table_Name).NewRow()
102
103
With dr<xsl:for-each select="FIELDS/FIELD">
104
.Item(FIELD_<xsl:value-of select="@Name" />) = i<xsl:apply-templates select="//TableName" />.<xsl:value-of select="@Name" />
105
</xsl:for-each>
106
End With
107
108
Tables(Table_Name).Rows.Add(dr)
109
110
Return Tables(Table_Name).Rows.Count
111
End Function
112
End Class
113
End Namespace</xsl:template>
114
<xsl:template match="//TableName">
115
<xsl:value-of select="@value" />
116
</xsl:template>
117
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8" ?>2
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">3
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />4
<xsl:template match="/Schema">Imports System.Runtime.Serialization5
6
Namespace Data7
<xsl:text disable-output-escaping="yes"><</xsl:text>System.ComponentModel.DesignerCategory("Code"), SerializableAttribute()<xsl:text disable-output-escaping="yes">></xsl:text> Public Class dsEntity<xsl:apply-templates select="//TableName" />8
Inherits DataSet9
'10
' table constants11
'12
Public Const Table_Name As String = "<xsl:apply-templates select="//TableName" />"13
'14
' feilds constants15
'16
<xsl:for-each select="FIELDS/FIELD">Public Const FIELD_<xsl:value-of select="@Name" /> As String = "<xsl:value-of select="@Name" />"17
</xsl:for-each>18

19
'----------------------------------------------------------------20
' Sub New:21
' Constructor to support serialization.22
'----------------------------------------------------------------23

24
Private Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)25
MyBase.New(info, context)26
End Sub27

28
'----------------------------------------------------------------29
' Sub New:30
' Initialize a CustomerData instance by building the table schema.31
'----------------------------------------------------------------32
Public Sub New()33
MyBase.New()34
'35
' Create the tables in the dataset36
'37
BuildDataTables()38
End Sub39

40
Public Sub New(ByVal i<xsl:apply-templates select="//TableName" /> As Entity<xsl:apply-templates select="//TableName" />)41
MyBase.New()42

43
'44
' Create the tables in the dataset45
'46
BuildDataTables()47
'48
Add(i<xsl:apply-templates select="//TableName" />)49
End Sub50

51
'----------------------------------------------------------------52
' Sub BuildDataTables:53
' Creates the datatables54
'----------------------------------------------------------------55
Private Sub BuildDataTables()56
'Create the tabe that will be returned57
' Create the Event table58
'59
Dim table As DataTable = New DataTable(Table_Name)60

61
'Create the tabe that will be returned62
' Create the Event column63
With table.Columns64
<xsl:for-each select="FIELDS/FIELD"> .Add(FIELD_<xsl:value-of select="@Name" />, GetType(<xsl:value-of select="@Type" />))65
</xsl:for-each>End With66

67
'add the table68
Me.Tables.Add(table)69
End Sub70
71
#Region "Properties"72
Default Public Property Item(ByVal index As Integer) As Entity<xsl:apply-templates select="//TableName" />73
Get74
Return getclbMember(index)75
End Get76
Set(ByVal Value As Entity<xsl:apply-templates select="//TableName" />)77
setclbMember(index, Value)78
End Set79
End Property80
81
Private Function getclbMember(ByVal index As Integer) As Entity<xsl:apply-templates select="//TableName" />82
Dim i<xsl:apply-templates select="//TableName" /> As New Entity<xsl:apply-templates select="//TableName" />83
With Tables(Table_Name).Rows(index)84
<xsl:for-each select="FIELDS/FIELD"> i<xsl:apply-templates select="//TableName" />.<xsl:value-of select="@Name" /> = .Item(FIELD_<xsl:value-of select="@Name" />)85
</xsl:for-each>End With86
Return i<xsl:apply-templates select="//TableName" />87
End Function88
89
Private Sub setclbMember(ByVal index As Integer, ByVal i<xsl:apply-templates select="//TableName" /> As Entity<xsl:apply-templates select="//TableName" />)90
With Tables(Table_Name).Rows(index)91
.BeginEdit()<xsl:for-each select="FIELDS/FIELD">92
.Item(FIELD_<xsl:value-of select="@Name" />) = i<xsl:apply-templates select="//TableName" />.<xsl:value-of select="@Name" />93
</xsl:for-each>94
.EndEdit()95
End With96
End Sub97
#End Region98

99
Public Function Add(ByVal i<xsl:apply-templates select="//TableName" /> As Entity<xsl:apply-templates select="//TableName" />) As Integer100
Dim dr As DataRow101
dr = Tables(Table_Name).NewRow()102

103
With dr<xsl:for-each select="FIELDS/FIELD">104
.Item(FIELD_<xsl:value-of select="@Name" />) = i<xsl:apply-templates select="//TableName" />.<xsl:value-of select="@Name" />105
</xsl:for-each>106
End With107

108
Tables(Table_Name).Rows.Add(dr)109

110
Return Tables(Table_Name).Rows.Count111
End Function112
End Class113
End Namespace</xsl:template>114
<xsl:template match="//TableName">115
<xsl:value-of select="@value" />116
</xsl:template>117
</xsl:stylesheet>同样的,使用上次的代码转换生成需要的代码:
1
Imports System.Runtime.Serialization
2
3
Namespace Data
4
<System.ComponentModel.DesignerCategory("Code"), SerializableAttribute()> Public Class dsEntityclbActivity
5
Inherits DataSet
6
'
7
' table constants
8
'
9
Public Const Table_Name As String = "clbActivity"
10
'
11
' feilds constants
12
'
13
Public Const FIELD_ActivityID As String = "ActivityID"
14
Public Const FIELD_ActivityDate As String = "ActivityDate"
15
Public Const FIELD_ActivityTheme As String = "ActivityTheme"
16
Public Const FIELD_ActivityAddress As String = "ActivityAddress"
17
18
19
'----------------------------------------------------------------
20
' Sub New:
21
' Constructor to support serialization.
22
'----------------------------------------------------------------
23
24
Private Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
25
MyBase.New(info, context)
26
End Sub
27
28
'----------------------------------------------------------------
29
' Sub New:
30
' Initialize a CustomerData instance by building the table schema.
31
'----------------------------------------------------------------
32
Public Sub New()
33
MyBase.New()
34
'
35
' Create the tables in the dataset
36
'
37
BuildDataTables()
38
End Sub
39
40
Public Sub New(ByVal iclbActivity As EntityclbActivity)
41
MyBase.New()
42
43
'
44
' Create the tables in the dataset
45
'
46
BuildDataTables()
47
'
48
Add(iclbActivity)
49
End Sub
50
51
'----------------------------------------------------------------
52
' Sub BuildDataTables:
53
' Creates the datatables
54
'----------------------------------------------------------------
55
Private Sub BuildDataTables()
56
'Create the tabe that will be returned
57
' Create the Event table
58
'
59
Dim table As DataTable = New DataTable(Table_Name)
60
61
'Create the tabe that will be returned
62
' Create the Event column
63
With table.Columns
64
.Add(FIELD_ActivityID, GetType(String))
65
.Add(FIELD_ActivityDate, GetType(String))
66
.Add(FIELD_ActivityTheme, GetType(String))
67
.Add(FIELD_ActivityAddress, GetType(String))
68
End With
69
70
'add the table
71
Me.Tables.Add(table)
72
End Sub
73
74
Properties
106
107
Public Function Add(ByVal iclbActivity As EntityclbActivity) As Integer
108
Dim dr As DataRow
109
dr = Tables(Table_Name).NewRow()
110
111
With dr
112
.Item(FIELD_ActivityID) = iclbActivity.ActivityID
113
.Item(FIELD_ActivityDate) = iclbActivity.ActivityDate
114
.Item(FIELD_ActivityTheme) = iclbActivity.ActivityTheme
115
.Item(FIELD_ActivityAddress) = iclbActivity.ActivityAddress
116
End With
117
118
Tables(Table_Name).Rows.Add(dr)
119
120
Return Tables(Table_Name).Rows.Count
121
End Function
122
End Class
123
End Namespace
Imports System.Runtime.Serialization2
3
Namespace Data4
<System.ComponentModel.DesignerCategory("Code"), SerializableAttribute()> Public Class dsEntityclbActivity5
Inherits DataSet6
'7
' table constants8
'9
Public Const Table_Name As String = "clbActivity"10
'11
' feilds constants12
'13
Public Const FIELD_ActivityID As String = "ActivityID"14
Public Const FIELD_ActivityDate As String = "ActivityDate"15
Public Const FIELD_ActivityTheme As String = "ActivityTheme"16
Public Const FIELD_ActivityAddress As String = "ActivityAddress"17
18

19
'----------------------------------------------------------------20
' Sub New:21
' Constructor to support serialization.22
'----------------------------------------------------------------23

24
Private Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)25
MyBase.New(info, context)26
End Sub27

28
'----------------------------------------------------------------29
' Sub New:30
' Initialize a CustomerData instance by building the table schema.31
'----------------------------------------------------------------32
Public Sub New()33
MyBase.New()34
'35
' Create the tables in the dataset36
'37
BuildDataTables()38
End Sub39

40
Public Sub New(ByVal iclbActivity As EntityclbActivity)41
MyBase.New()42

43
'44
' Create the tables in the dataset45
'46
BuildDataTables()47
'48
Add(iclbActivity)49
End Sub50

51
'----------------------------------------------------------------52
' Sub BuildDataTables:53
' Creates the datatables54
'----------------------------------------------------------------55
Private Sub BuildDataTables()56
'Create the tabe that will be returned57
' Create the Event table58
'59
Dim table As DataTable = New DataTable(Table_Name)60

61
'Create the tabe that will be returned62
' Create the Event column63
With table.Columns64
.Add(FIELD_ActivityID, GetType(String))65
.Add(FIELD_ActivityDate, GetType(String))66
.Add(FIELD_ActivityTheme, GetType(String))67
.Add(FIELD_ActivityAddress, GetType(String))68
End With69

70
'add the table71
Me.Tables.Add(table)72
End Sub73
74
Properties106

107
Public Function Add(ByVal iclbActivity As EntityclbActivity) As Integer108
Dim dr As DataRow109
dr = Tables(Table_Name).NewRow()110

111
With dr112
.Item(FIELD_ActivityID) = iclbActivity.ActivityID113
.Item(FIELD_ActivityDate) = iclbActivity.ActivityDate114
.Item(FIELD_ActivityTheme) = iclbActivity.ActivityTheme115
.Item(FIELD_ActivityAddress) = iclbActivity.ActivityAddress116
End With117

118
Tables(Table_Name).Rows.Add(dr)119

120
Return Tables(Table_Name).Rows.Count121
End Function122
End Class123
End Namespace有了实体类和序列化的dataset类,后面我们就可以生成对数据进行增删改查询的数据操作类啦!
相关链接: 使用XML + XSLT进行实体类代码生成 (一)
使用XML + XLST进行实体类代码生成 (二)
使用XML + XLST进行实体类代码生成 (三)

浙公网安备 33010602011771号