使用XML + XLST进行实体类代码生成 (三)
还是赶快把内容介绍完,否则看完了前两帖的兄弟们还不知道我要做什么呢。
四,使用XslTransform类来生成代码
这一步就是最后的输出啦,其实也很简单。但重要的是想要要的东西就考这一步输出了。
工作原理很简单就是将上一步中生成的xslt模板,与之前生成XML数据进行一个Transform就行了。
运行的结果如下:
得到的代码:
四,使用XslTransform类来生成代码
这一步就是最后的输出啦,其实也很简单。但重要的是想要要的东西就考这一步输出了。
工作原理很简单就是将上一步中生成的xslt模板,与之前生成XML数据进行一个Transform就行了。
1 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
2 Dim stringWriter As System.IO.StringWriter = New System.IO.StringWriter
3 Dim xdoc As New XmlDocument
4 Dim strXML As String = "<?xml version=""1.0"" encoding=""utf-8"" ?><Schema></Schema>"
5 Dim xslt As Xsl.XslTransform = New Xsl.XslTransform
6
7 xdoc.LoadXml(strXML)
8 xdoc.DocumentElement.InnerXml = TextBox1.Text
9
10 xslt.Load("E:\Project\Lab\ORMap\ORMap\xml\tableTemplate.xslt")
11
12 xslt.Transform(xdoc, Nothing, stringWriter, Nothing)
13
14 TextBox1.Text = stringWriter.ToString
15 End Sub
2 Dim stringWriter As System.IO.StringWriter = New System.IO.StringWriter
3 Dim xdoc As New XmlDocument
4 Dim strXML As String = "<?xml version=""1.0"" encoding=""utf-8"" ?><Schema></Schema>"
5 Dim xslt As Xsl.XslTransform = New Xsl.XslTransform
6
7 xdoc.LoadXml(strXML)
8 xdoc.DocumentElement.InnerXml = TextBox1.Text
9
10 xslt.Load("E:\Project\Lab\ORMap\ORMap\xml\tableTemplate.xslt")
11
12 xslt.Transform(xdoc, Nothing, stringWriter, Nothing)
13
14 TextBox1.Text = stringWriter.ToString
15 End Sub
运行的结果如下:
得到的代码:
1Namespace Data
2 Public Class EntityclbActivity
3
4 'variable
5 dim m_ActivityID as String
6 dim m_ActivityDate as String
7 dim m_ActivityTheme as String
8 dim m_ActivityAddress as String
9
10 Public Sub New()
11
12 End Sub
13
14 Public Sub New(Byval pActivityID as String)
15
16 m_ActivityID = pActivityID
17 End Sub
18
19 Public Sub New(Byval pActivityID as String, Byval pActivityDate as String, Byval pActivityTheme as String, Byval pActivityAddress as String)
20
21 m_ActivityID = pActivityID
22 m_ActivityDate = pActivityDate
23 m_ActivityTheme = pActivityTheme
24 m_ActivityAddress = pActivityAddress
25 End Sub
26
27 Public Property ActivityID() As String
28 Get
29 Return m_ActivityID
30 End Get
31 Set(ByVal Value As String)
32 m_ActivityID = Value
33 End Set
34 End Property
35
36 Public Property ActivityDate() As String
37 Get
38 Return m_ActivityDate
39 End Get
40 Set(ByVal Value As String)
41 m_ActivityDate = Value
42 End Set
43 End Property
44
45 Public Property ActivityTheme() As String
46 Get
47 Return m_ActivityTheme
48 End Get
49 Set(ByVal Value As String)
50 m_ActivityTheme = Value
51 End Set
52 End Property
53
54 Public Property ActivityAddress() As String
55 Get
56 Return m_ActivityAddress
57 End Get
58 Set(ByVal Value As String)
59 m_ActivityAddress = Value
60 End Set
61 End Property
62
63 End Class
64End Namespace
2 Public Class EntityclbActivity
3
4 'variable
5 dim m_ActivityID as String
6 dim m_ActivityDate as String
7 dim m_ActivityTheme as String
8 dim m_ActivityAddress as String
9
10 Public Sub New()
11
12 End Sub
13
14 Public Sub New(Byval pActivityID as String)
15
16 m_ActivityID = pActivityID
17 End Sub
18
19 Public Sub New(Byval pActivityID as String, Byval pActivityDate as String, Byval pActivityTheme as String, Byval pActivityAddress as String)
20
21 m_ActivityID = pActivityID
22 m_ActivityDate = pActivityDate
23 m_ActivityTheme = pActivityTheme
24 m_ActivityAddress = pActivityAddress
25 End Sub
26
27 Public Property ActivityID() As String
28 Get
29 Return m_ActivityID
30 End Get
31 Set(ByVal Value As String)
32 m_ActivityID = Value
33 End Set
34 End Property
35
36 Public Property ActivityDate() As String
37 Get
38 Return m_ActivityDate
39 End Get
40 Set(ByVal Value As String)
41 m_ActivityDate = Value
42 End Set
43 End Property
44
45 Public Property ActivityTheme() As String
46 Get
47 Return m_ActivityTheme
48 End Get
49 Set(ByVal Value As String)
50 m_ActivityTheme = Value
51 End Set
52 End Property
53
54 Public Property ActivityAddress() As String
55 Get
56 Return m_ActivityAddress
57 End Get
58 Set(ByVal Value As String)
59 m_ActivityAddress = Value
60 End Set
61 End Property
62
63 End Class
64End Namespace
将生成的类反向工程到visio中,
哈哈!需要的类是不是已经生成出来了。今后,只需要修改xlst文件就可以生成新的模板,生成需要的类。
办法虽然很简单,但重要的是看怎么应用了。后面还会跟进的讨论:
1. 使用xlst文件生成与实体类相关的自定义Dataset。
2. 生成带有增删改查询方法的DO对象。
另,在这个过程中有小小的偷懒,就是在生成XML的时候没有对数据表的数据类型做到.Net数据类型的映射。(小小的偷懒,大家用的时候要将这部分补上哦!有好的映射方法别忘了告诉我一声。)
相关链接: 使用XML + XSLT进行实体类代码生成 (一)
使用XML + XLST进行实体类代码生成 (二)