CodeSmith实用技巧(六):使用XML 属性

  CodeSmith 允许我们存储元数据在 XML 文件中,然后在执行模版时直接打开 XML 文件填写到属性面板中。 <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

1XML Property With a Schema

 1 None.gif <? xml version="1.0" encoding="UTF-8" ?>
 2 None.gif < xs:schema  targetNamespace =http://www.codesmithtools.com/PO
 3 None.gif      xmlns:xs =http://www.w3.org/2001/XMLSchema
 4 None.gif      xmlns =http://www.codesmithtools.com/PO
 5 None.gif      elementFormDefault ="qualified"  attributeFormDefault ="unqualified" >
 6 None.gif    < xs:element  name ="PurchaseOrder" >
 7 None.gif      < xs:complexType >
 8 None.gif        < xs:sequence >
 9 None.gif          < xs:element  name ="PONumber"  type ="xs:string" />
10 None.gif          < xs:element  name ="CustomerName"  type ="xs:string" />
11 None.gif          < xs:element  name ="CustomerCity"  type ="xs:string" />
12 None.gif          < xs:element  name ="CustomerState"  type ="xs:string" />
13 None.gif          < xs:element  name ="Items" >
14 None.gif           < xs:complexType >
15 None.gif             < xs:sequence >
16 None.gif               < xs:element  name ="Item"  maxOccurs ="unbounded" >
17 None.gif                 < xs:complexType >
18 None.gif                   < xs:attribute  name ="ItemNumber"  type ="xs:string"  use ="required" />
19 None.gif                   < xs:attribute  name ="Quantity"  type ="xs:integer"  use ="required" />
20 None.gif                 </ xs:complexType >
21 None.gif               </ xs:element >
22 None.gif             </ xs:sequence >
23 None.gif           </ xs:complexType >
24 None.gif          </ xs:element >
25 None.gif        </ xs:sequence >
26 None.gif      </ xs:complexType >
27 None.gif    </ xs:element >
28 None.gif </ xs:schema >
29 None.gif
30 None.gif

这是一个简单的带有SchemaXML Property的例子:

利用这个Schema文件,我们可以定义一个XML Property来在运行时读去元数据。

None.gif <% @ CodeTemplate Language = " C# "  TargetLanguage = " Text "  Description = " Create packing list from XML PO. "   %>
None.gif
<% @ XmlProperty Name = " PurchaseOrder "  Schema = " PO.xsd "  Optional = " False "  Category = " Data "  Description = " Purchase Order to generate packing list for. "   %>
None.gifPacking List
None.gif
ref : PO# <%=  PurchaseOrder.PONumber  %>
None.gifShip To:
None.gif
<%=  PurchaseOrder.CustomerName  %>
None.gif
<%=  PurchaseOrder.CustomerCity   %> <%=  PurchaseOrder.CustomerState  %>
None.gifContents:
ExpandedBlockStart.gifContractedBlock.gif
<%   for  ( int  i  =   0 ; i  <  PurchaseOrder.Items.Count; i ++ dot.gif %>
InBlock.gif
<%= PurchaseOrder.Items[i].ItemNumber %>, Quantity <%= PurchaseOrder.Items[i].Quantity %>
ExpandedBlockEnd.gif
<% }
  %>
None.gif
None.gif

在运行时,PurchaseOrder属性在属性面板中显示为按钮,单击后弹出一个对话框供用户选择XML文件。

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />PIC059.JPG

选择一个XML文件。在该例子XML文件内容如下:

 1 None.gif <? xml version="1.0" encoding="UTF-8" ?>
 2 None.gif < PurchaseOrder  xmlns =http://www.codesmithtools.com/PO
 3 None.gif      xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
 4 None.gif    < PONumber > 5271 </ PONumber >
 5 None.gif    < CustomerName > John Nelson </ CustomerName >
 6 None.gif    < CustomerCity > Gamonetta </ CustomerCity >
 7 None.gif    < CustomerState > MS </ CustomerState >
 8 None.gif    < Items >
 9 None.gif      < Item  ItemNumber ="HM85"  Quantity ="12" />
10 None.gif      < Item  ItemNumber ="JR82"  Quantity ="4" />
11 None.gif      < Item  ItemNumber ="PR43"  Quantity ="6" />
12 None.gif    </ Items >
13 None.gif </ PurchaseOrder >
14 None.gif
15 None.gif

生成后的代码如下:

None.gif Packing List
None.gifref: PO#5271
None.gifShip To:
None.gifJohn Nelson
None.gifGamonetta, MS
None.gifContents:
None.gifHM85, Quantity 12
None.gifJR82, Quantity 4
None.gifPR43, Quantity 6

2XML Property Without a Schema

这是一个不带SchemaXML Property的例子。这个模版在运行时可以访问任何XML文件。

None.gif <% @ CodeTemplate Language = " VB "  TargetLanguage = " Text "  Description = " List top-level nodes in an XML file. "   %>
None.gif
<% @ XmlProperty Name = " TargetFile "  Optional = " False "  Category = " Data "  Description = " XML file to iterate. "   %>
None.gif
<% @ Assembly Name = " System.Xml "   %>
None.gif
<% @ Import Namespace = " System.Xml "   %>
None.gifTop
- level nodes:
None.gif
<%  Dim currNode  as  XmlNode
None.gifcurrNode 
=  TargetFile.DocumentElement.FirstChild
None.gifDo Until currNode Is Nothing
%>
None.gif   
<%=  currNode.InnerXml  %>
None.gif
<%  currNode  =  currNode.NextSibling()
None.gifLoop 
%>
None.gif
None.gif

概莫版对目标文件的属性并没有定义一个Schema,所以属性在模版中是作为一个XMLDocument。如果我们选择的XML文件如下所示:

1 None.gif <? xml version="1.0" encoding="UTF-8" ?>
2 None.gif < Books >
3 None.gif    < Book > UML 2.0 In a Nutshell </ Book >
4 None.gif    < Book > The Best Software Writing </ Book >
5 None.gif    < Book > Coder to Developer </ Book >
6 None.gif    < Book > Code Complete </ Book >
7 None.gif </ Books >

生成后的代码:

None.gif Top-level nodes:
None.gifUML 2.0 In a Nutshell
None.gifThe Best Software Writing
None.gifCoder to Developer
None.gifCode Complete
None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值