[C #] Step by step, develop your own Automatic Code Generation Tool 3: code generation engine

Source: Internet
Author: User

Step 3:CodeGeneration Engine

There are many automatic code generation solutions. Here, the most common XML + XSLT template method is used for the convenience of template configuration.

The core code is as follows:

    
Public static bool transferxml (xmldocument xmldoc, string export path, string targetfilename) {export compiledtransform export TRAN = new export (); xmltextwriter XW = NULL; try {XW = new xmltextwriter (targetfilename, encoding. utf8); Using Tran. load (export path); export Tran. transform (xmldoc, XW); Return true;} catch {return false;} finally {If (XW! = NULL) {XW. flush (); XW. close () ;}} public static string transferxmltostring (xmldocument xmldoc, string effectpath) {// get model class template string resultstr = ""; memorystream MS = NULL; streamreader sr = NULL; try {export compiledtransform xtran = new export compiledtransform (); xtran. load (effectpath); MS = new system. io. memorystream (); xpathnavigator nav = xmldoc. createnavigator (); xtran. transform (NAV, null, MS); m S. Position = 0; Sr = new streamreader (MS); resultstr = Sr. readtoend (); resultstr = resultstr. Replace ("<? XML version =/"1.0/" encoding =/"UTF-8/"?> "," "); Return resultstr;} catch {return" ";}finally {If (SR! = NULL) {Sr. Close (); Sr. Dispose ();} If (MS! = NULL) {Ms. Dispose ();}}}

 

Now, the code generation tool is complete.

 

 

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.