May 25, 2021 CodeSmith
In the previous CodeSmith tutorial (6): Basic syntax-declaration and use of properties Describes the basic methods of using properties in CodeSmith, which are defined by Property instructions.
CodeSmith also supports the use of XML documentation to define properties, some configuration properties can be defined into XML files, and the properties that define XML are defined using XmlProperty:
<%@ XmlProperty Name="PurchaseOrder"
Schema="PO.xsd"
Optional="False"
Category="Data"
Description="Purchase Order to generate packing list for." %>
XmlProperty instructions can have multiple parameters, and references are optional except name as a must.
Introduction to property parameters:
Using an example from CodeSmith, using PurchaseOrder.xsd, XML is defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://www.codesmithtools.com/purchaseorder"
elementFormDefault="qualified"
xmlns="http://www.codesmithtools.com/purchaseorder"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="PurchaseOrder">
<xs:complexType>
<xs:sequence>
<xs:element name="OrderDate" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="SubTotal" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="ShipCost" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="TotalCost" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="ShipTo" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="Line1" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="City" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="State" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="Zip" type="xs:string" minOccurs="0" maxOccurs="1" />
</xs:sequence>
<xs:attribute name="Name" type="xs:string" />
</xs:complexType>
</xs:element>
<xs:element name="Items" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="OrderedItem" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="ItemName" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="Description" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="UnitPrice" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="Quantity" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="LineTotal" type="xs:string" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The XML file used with this XML Schema to hold the XML properties is samplePurchaseOrder .xml defined as follows:
<?xml version="1.0"?>
<PurchaseOrder xmlns="http://www.codesmithtools.com/purchaseorder">
<ShipTo Name="Eric J. Smith">
<Line1>123 Test Dr.</Line1>
<City>Dallas</City>
<State>TX</State>
<Zip>75075</Zip>
</ShipTo>
<OrderDate>05-01-2003</OrderDate>
<Items>
<OrderedItem>
<ItemName>Item #1</ItemName>
<Description>Item #1 Description</Description>
<UnitPrice>5.45</UnitPrice>
<Quantity>3</Quantity>
<LineTotal>16.35</LineTotal>
</OrderedItem>
<OrderedItem>
<ItemName>Item #2</ItemName>
<Description>Item #2 Description</Description>
<UnitPrice>12.75</UnitPrice>
<Quantity>8</Quantity>
<LineTotal>102.00</LineTotal>
</OrderedItem>
</Items>
<SubTotal>45.23</SubTotal>
<ShipCost>5.23</ShipCost>
<TotalCost>50.46</TotalCost>
</PurchaseOrder>
Define a simple template to re-output the contents of samplePurchaseOrder.xml, and you can define an XMLProperty in the code template, with Schema designated as PurchaseOrder.xsd
<%--
This template demonstates using the XmlProperty directive
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="Text"
Description="Demonstrates using the Xml serializer." %>
<%@ XmlProperty
Name="MyPurchaseOrder"
Schema="PurchaseOrder.xsd"
Default="SamplePurchaseOrder.xml" %>
This file generated by CodeSmith on <%= DateTime.Now.ToLongDateString() %>
PurchaseOrder:
Address:
Name: <%= MyPurchaseOrder.ShipTo.Name %>
Line1: <%= MyPurchaseOrder.ShipTo.Line1 %>
City: <%= MyPurchaseOrder.ShipTo.City %>
State: <%= MyPurchaseOrder.ShipTo.State %>
Zip: <%= MyPurchaseOrder.ShipTo.Zip %>
OrderDate: <%= MyPurchaseOrder.OrderDate %>
Items:
<% for (int i = 0; i < MyPurchaseOrder.Items.Count; i++) { %>
<%= i %>:
ItemName: <%= MyPurchaseOrder.Items[i].ItemName %>
Description: <%= MyPurchaseOrder.Items[i].Description %>
UnitPrice: <%= MyPurchaseOrder.Items[i].UnitPrice %>
Quantity: <%= MyPurchaseOrder.Items[i].Quantity %>
LineTotal: <%= MyPurchaseOrder.Items[i].LineTotal %>
<% } %>
SubTotal: <%= MyPurchaseOrder.SubTotal %>
ShipCost: <%= MyPurchaseOrder.ShipCost %>
TotalCost: <%= MyPurchaseOrder.TotalCost %>
The XML property defined in the template is named MyPurchaseOrder and Schema is PurchaseOrder.xsd, so the elements defined in XML Schema can be referenced directly in the code template in the format of MyPurchaseOrder.ShipTo.Name, and CoddSmith also supports IntelliSense.
To run the template, you first need to select the right XML file for MyPurchaseOrder:
If the selected file does not conform to the specified XML Schema, CodeSmith does not allow the selection of the file, in this case using the predefined Sample PurchaseOrder .xml, the resulting file is as follows:
This file generated by CodeSmith on Saturday, 12 January 2013
PurchaseOrder:
Address:
Name: Eric J. Smith
Line1: 123 Test Dr.
City: Dallas
State: TX
Zip: 75075
OrderDate: 05-01-2003
Items:
0:
ItemName: Item #1
Description: Item #1 Description
UnitPrice: 5.45
Quantity: 3
LineTotal: 16.35
1:
ItemName: Item #2
Description: Item #2 Description
UnitPrice: 12.75
Quantity: 8
LineTotal: 102.00
SubTotal: 45.23
ShipCost: 5.23
TotalCost: 50.46
This example Downloads