CodeSmith实用技巧(六):使用XML 属性
CodeSmith允许我们存储元数据在XML文件中,然后在执行模版时直接打开XML文件填写到属性面板中。
1.XML Property With a Schema
1<?xml version="1.0" encoding="UTF-8"?>
2<xs:schema targetNamespace=http://www.codesmithtools.com/PO
3 xmlns:xs=http://www.w3.org/2001/XMLSchema
4 xmlns=http://www.codesmithtools.com/PO
5 elementFormDefault="qualified" attributeFormDefault="unqualified">
6 <xs:element name="PurchaseOrder">
7 <xs:complexType>
8 <xs:sequence>
9 <xs:element name="PONumber" type="xs:string"/>
10 <xs:element name="CustomerName" type="xs:string"/>
11 <xs:element name="CustomerCity" type="xs:string"/>
12 <xs:element name="CustomerState" type="xs:string"/>
13 <xs:element name="Items">
14 <xs:complexType>
15 <xs:sequence>
16 <xs:element name="Item" maxOccurs="unbounded">
17 <xs:complexType>
18 <xs:attribute name="ItemNumber" type="xs:string" use="required"/>
19 <xs:attribute name="Quantity" type="xs:integer" use="required"/>
20 </xs:complexType>
21 </xs:element>
22 </xs:sequence>
23 </xs:complexType>
24 </xs:element>
25 </xs:sequence>
26 </xs:complexType>
27 </xs:element>
28</xs:schema>
29
30
2<xs:schema targetNamespace=http://www.codesmithtools.com/PO
3 xmlns:xs=http://www.w3.org/2001/XMLSchema
4 xmlns=http://www.codesmithtools.com/PO
5 elementFormDefault="qualified" attributeFormDefault="unqualified">
6 <xs:element name="PurchaseOrder">
7 <xs:complexType>
8 <xs:sequence>
9 <xs:element name="PONumber" type="xs:string"/>
10 <xs:element name="CustomerName" type="xs:string"/>
11 <xs:element name="CustomerCity" type="xs:string"/>
12 <xs:element name="CustomerState" type="xs:string"/>
13 <xs:element name="Items">
14 <xs:complexType>
15 <xs:sequence>
16 <xs:element name="Item" maxOccurs="unbounded">
17 <xs:complexType>
18 <xs:attribute name="ItemNumber" type="xs:string" use="required"/>
19 <xs:attribute name="Quantity" type="xs:integer" use="required"/>
20 </xs:complexType>
21 </xs:element>
22 </xs:sequence>
23 </xs:complexType>
24 </xs:element>
25 </xs:sequence>
26 </xs:complexType>
27 </xs:element>
28</xs:schema>
29
30
这是一个简单的带有Schema的XML Property的例子:
利用这个Schema文件,我们可以定义一个XML Property来在运行时读去元数据。
<%@ CodeTemplate Language="C#" TargetLanguage="Text" Description="Create packing list from XML PO." %>
<%@ XmlProperty Name="PurchaseOrder" Schema="PO.xsd" Optional="False" Category="Data" Description="Purchase Order to generate packing list for." %>
Packing List
ref: PO#<%= PurchaseOrder.PONumber %>
Ship To:
<%= PurchaseOrder.CustomerName %>
<%= PurchaseOrder.CustomerCity %>, <%= PurchaseOrder.CustomerState %>
Contents:
<% for (int i = 0; i < PurchaseOrder.Items.Count; i++) { %>
<%= PurchaseOrder.Items[i].ItemNumber %>, Quantity <%= PurchaseOrder.Items[i].Quantity %>
<% } %>
<%@ XmlProperty Name="PurchaseOrder" Schema="PO.xsd" Optional="False" Category="Data" Description="Purchase Order to generate packing list for." %>
Packing List
ref: PO#<%= PurchaseOrder.PONumber %>
Ship To:
<%= PurchaseOrder.CustomerName %>
<%= PurchaseOrder.CustomerCity %>, <%= PurchaseOrder.CustomerState %>
Contents:
<% for (int i = 0; i < PurchaseOrder.Items.Count; i++) { %>
<%= PurchaseOrder.Items[i].ItemNumber %>, Quantity <%= PurchaseOrder.Items[i].Quantity %>
<% } %>
在运行时,PurchaseOrder属性在属性面板中显示为按钮,单击后弹出一个对话框供用户选择XML文件。
选择一个XML文件。在该例子XML文件内容如下:
1<?xml version="1.0" encoding="UTF-8"?>
2<PurchaseOrder xmlns=http://www.codesmithtools.com/PO
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4 <PONumber>5271</PONumber>
5 <CustomerName>John Nelson</CustomerName>
6 <CustomerCity>Gamonetta</CustomerCity>
7 <CustomerState>MS</CustomerState>
8 <Items>
9 <Item ItemNumber="HM85" Quantity="12"/>
10 <Item ItemNumber="JR82" Quantity="4"/>
11 <Item ItemNumber="PR43" Quantity="6"/>
12 </Items>
13</PurchaseOrder>
14
15
2<PurchaseOrder xmlns=http://www.codesmithtools.com/PO
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4 <PONumber>5271</PONumber>
5 <CustomerName>John Nelson</CustomerName>
6 <CustomerCity>Gamonetta</CustomerCity>
7 <CustomerState>MS</CustomerState>
8 <Items>
9 <Item ItemNumber="HM85" Quantity="12"/>
10 <Item ItemNumber="JR82" Quantity="4"/>
11 <Item ItemNumber="PR43" Quantity="6"/>
12 </Items>
13</PurchaseOrder>
14
15
生成后的代码如下:
Packing List
ref: PO#5271
Ship To:
John Nelson
Gamonetta, MS
Contents:
HM85, Quantity 12
JR82, Quantity 4
PR43, Quantity 6
ref: PO#5271
Ship To:
John Nelson
Gamonetta, MS
Contents:
HM85, Quantity 12
JR82, Quantity 4
PR43, Quantity 6
2.XML Property Without a Schema
这是一个不带Schema的XML Property的例子。这个模版在运行时可以访问任何XML文件。
<%@ CodeTemplate Language="VB" TargetLanguage="Text" Description="List top-level nodes in an XML file." %>
<%@ XmlProperty Name="TargetFile" Optional="False" Category="Data" Description="XML file to iterate." %>
<%@ Assembly Name="System.Xml" %>
<%@ Import Namespace="System.Xml" %>
Top-level nodes:
<% Dim currNode as XmlNode
currNode = TargetFile.DocumentElement.FirstChild
Do Until currNode Is Nothing%>
<%= currNode.InnerXml %>
<% currNode = currNode.NextSibling()
Loop %>
<%@ XmlProperty Name="TargetFile" Optional="False" Category="Data" Description="XML file to iterate." %>
<%@ Assembly Name="System.Xml" %>
<%@ Import Namespace="System.Xml" %>
Top-level nodes:
<% Dim currNode as XmlNode
currNode = TargetFile.DocumentElement.FirstChild
Do Until currNode Is Nothing%>
<%= currNode.InnerXml %>
<% currNode = currNode.NextSibling()
Loop %>
概莫版对目标文件的属性并没有定义一个Schema,所以属性在模版中是作为一个XMLDocument。如果我们选择的XML文件如下所示:
1<?xml version="1.0" encoding="UTF-8"?>
2<Books>
3 <Book>UML 2.0 In a Nutshell</Book>
4 <Book>The Best Software Writing</Book>
5 <Book>Coder to Developer</Book>
6 <Book>Code Complete</Book>
7</Books>
2<Books>
3 <Book>UML 2.0 In a Nutshell</Book>
4 <Book>The Best Software Writing</Book>
5 <Book>Coder to Developer</Book>
6 <Book>Code Complete</Book>
7</Books>
生成后的代码:
Top-level nodes:
UML 2.0 In a Nutshell
The Best Software Writing
Coder to Developer
Code Complete
UML 2.0 In a Nutshell
The Best Software Writing
Coder to Developer
Code Complete
支持TerryLee的创业产品Worktile
Worktile,新一代简单好用、体验极致的团队协同、项目管理工具,让你和你的团队随时随地一起工作。完全免费,现在就去了解一下吧。
https://worktile.com
Worktile,新一代简单好用、体验极致的团队协同、项目管理工具,让你和你的团队随时随地一起工作。完全免费,现在就去了解一下吧。
https://worktile.com