示例#1
0
        public static void GenerateXObjects(
            XmlSchemaSet set,
            string csFileName,
            string configFileName,
            string assemblyName,
            bool xmlSerializable,
            bool nameMangler2)
        {
            LinqToXsdSettings configSettings = new LinqToXsdSettings(nameMangler2);
            if (configFileName != null)
            {
                configSettings.Load(configFileName);
            }
            configSettings.EnableServiceReference = xmlSerializable;
            XsdToTypesConverter xsdConverter = new XsdToTypesConverter(configSettings);
            ClrMappingInfo mapping = xsdConverter.GenerateMapping(set);

            CodeDomTypesGenerator codeGenerator = new CodeDomTypesGenerator(configSettings);
            CodeCompileUnit ccu = new CodeCompileUnit();
            foreach(var codeNs in codeGenerator.GenerateTypes(mapping))
            {
                ccu.Namespaces.Add(codeNs);
            }
            //Write to file
            CSharpCodeProvider provider = new CSharpCodeProvider();
            if (csFileName != string.Empty && csFileName != null)
            {
                using (var update =
                    new Update(csFileName, System.Text.Encoding.UTF8))
                {
                    provider.GenerateCodeFromCompileUnit(
                        ccu, update.Writer, new CodeGeneratorOptions());
                }
                PrintMessage(csFileName);
            }
            if(assemblyName != string.Empty)
            {
                var options = new CompilerParameters()
                {
                    OutputAssembly = assemblyName,
                    IncludeDebugInformation = true,
                    TreatWarningsAsErrors = true,
                };
                options.TempFiles.KeepFiles = true;
                {
                    var r = options.ReferencedAssemblies;
                    r.Add("System.dll");
                    r.Add("System.Core.dll");
                    r.Add("System.Xml.dll");
                    r.Add("System.Xml.Linq.dll");
                    r.Add("Xml.Schema.Linq.dll");
                }
                var results = provider.CompileAssemblyFromDom(options, ccu);
                if (results.Errors.Count > 0)
                {
                    PrintErrorMessage("compilation error(s): ");
                    for (int i = 0; i < results.Errors.Count; i++)
                    {
                        PrintErrorMessage(results.Errors[i].ToString());
                    }
                    throw new Exception("compilation error(s)");
                }
                else
                {
                    PrintMessage(
                        "Generated Assembly: " +
                        results.CompiledAssembly.ToString());
                }
            }
        }
示例#2
0
        static void GenerateXObjects(
            XmlSchemaSet set, string csFileName, string configFileName, string assemblyName, bool xmlSerializable, bool nameMangler2) 
        {
            LinqToXsdSettings configSettings = new LinqToXsdSettings(nameMangler2);
            if (configFileName != null) {
                configSettings.Load(configFileName);
            }
            configSettings.EnableServiceReference = xmlSerializable;
            XsdToTypesConverter xsdConverter = new XsdToTypesConverter(configSettings);
            ClrMappingInfo mapping = xsdConverter.GenerateMapping(set);

            CodeDomTypesGenerator codeGenerator = new CodeDomTypesGenerator(configSettings);
            CodeCompileUnit ccu = new CodeCompileUnit();
         //   if (mapping != null)                            //DC
                foreach(CodeNamespace codeNs in codeGenerator.GenerateTypes(mapping)) {
                    ccu.Namespaces.Add(codeNs);
            }
            //Write to file
            CSharpCodeProvider provider = new CSharpCodeProvider();
            if (csFileName != string.Empty)
            {
                /*
                StreamWriter sw = new StreamWriter(csFileName);
                provider.GenerateCodeFromCompileUnit(ccu, sw, new CodeGeneratorOptions());
                sw.Flush(); 
                sw.Close();
                 * */
                using (var update = 
                    new Update(csFileName, System.Text.Encoding.UTF8))
                {
                    provider.GenerateCodeFromCompileUnit(
                        ccu, update.Writer, new CodeGeneratorOptions());
                }                
                PrintMessage(csFileName);
            }
            if(assemblyName != string.Empty)
            {
                CompilerParameters options = new CompilerParameters();
                options.OutputAssembly = assemblyName;
                options.IncludeDebugInformation = true;
                options.TreatWarningsAsErrors = true;
                options.ReferencedAssemblies.Add("System.dll");
                options.ReferencedAssemblies.Add("System.Core.dll");
                options.ReferencedAssemblies.Add("System.Xml.dll");
                options.ReferencedAssemblies.Add("System.Xml.Linq.dll");
                options.ReferencedAssemblies.Add("O2_Misc_Microsoft_MPL_Libs.dll");
                CompilerResults results = provider.CompileAssemblyFromDom(options, ccu);
                if (results.Errors.Count > 0)
                {
                    PrintErrorMessage("compilation error(s): ");
                    for (int i = 0; i < results.Errors.Count; i++)
                        PrintErrorMessage(results.Errors[i].ToString());
                }
                else
                {
                    PrintMessage("Generated Assembly: " + results.CompiledAssembly.ToString());
                }
            };
        }