C # Discovery Tour third, using C # to develop an XSLT based code generator (2)

Source: Internet
Author: User
Tags generator serialization xmlns xslt

In this demo, we just use a Access2000 database in the program directory as an example, so we call only the LoadFromAccesss2000 function, and other functions that analyze SQL Server and Oracle are useless. In the future, when the code generator is improved and put into practical use, it can analyze enterprise databases such as SQL Server and Oracle.

A getxmlstring function is defined in the main page xslcreatecode.aspx, which serializes an object into an XML document. Both Databaseinfo,tableinfo and FieldInfo can be serialized in XML. When performing XML serialization, the system analyzes the object type. Iterate through all of the exposed fields and read-write properties of the object, and then output the property values to an XML document, which, if it encounters an object tree structure, recursively iterates through the tree structure, creating an XML child element for each subordinate object in the object. Here Databaseinfo,tableinfo and FieldInfo form a three-layer tree structure, so the resulting XML document is multi-layered.

In general, in the serialized XML document, the XML element name equals the name of the object type and exposes the name of the field property, but you can change this default behavior by adding attributes, preceded by the definition of the type Tableinfo XmlType The XML element name generated for the type Tableinfo is not an object type name Tableinfo, but a table.

[System.Xml.Serialization.XmlType("Table")]
public class TableInfo

In the same way, we specified the XML element name field for the type FieldInfo, which shows the application of the attribute in C #. About features in a future lesson.

Because attributes that can perform XML serialization must be read-write, so properties such as Isstring,isinteger in type FieldInfo define a useless set method in order to perform XML serialization.

XSLT Template Description

The program directory has a file with an XSLT extension that starts with an underscore, which is the code generation template used by the code generator. Using different templates in the main interface can generate different code. Here we take _cshaprhashtable.xslt as an example to illustrate.

_cshaprhashtable.xslt

First we select the data table customers in the interface and can generate its XML code as.

<table xmlns:xsd= "Http://www.w3.org/2001/XMLSchema"
xmlns:xsi= http://www.w3.org/2001/ Xmlschema-instance "
<name>customers</name>
<fields>
<field>
<N      Ame>address</name>
<Remark> address </remark>
<fieldtype>char</fieldtype>
<isstring>true</isstring>
<isinteger>false</isinteger>
<IsBoolean>fal Se</isboolean>
<isnumberic>false</isnumberic>
<ISDATETIME>FALSE</ISDATETIME&G T     
<isbinary>false</isbinary>
<valuetypename>system.string</valuetypename>
<fieldwidth>60</fieldwidth>
<nullable>true</nullable>
<primarykey>false& Lt;/primarykey>
<indexed>false</indexed>
</field>
<field>
< Name>city</name>
<remArk> City </remark>
<fieldtype>char</fieldtype>
<isstring>true</isstring> <isinteger>false</isinteger>
<isboolean>false</isboolean>
<IsNumberic >false</isnumberic>
<isdatetime>false</isdatetime>
<isbinary>false</isbina Ry>
<valuetypename>system.string</valuetypename>
<fieldwidth>15</fieldwidth>< br> <nullable>true</nullable>
<primarykey>false</primarykey>
<Indexed> ; False</indexed>
</field>
<Field> other fields ... </field>
</fields>
</ta Ble>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.