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.