コード例 #1
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();

            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();
                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("Xml.Schema.Linq.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());
                }
            }
            ;
        }
コード例 #2
0
        /// <summary>
        /// Creates a <see cref="CodeCompileUnit"/> from a given <see cref="XmlSchemaSet"/> and <see cref="LinqToXsdSettings"/>.
        /// </summary>
        /// <param name="schemaSet"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="schemaSet"/> is <see langword="null"/></exception>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="settings"/> is <see langword="null"/></exception>
        public static CodeCompileUnit GenerateCodeCompileUnit(XmlSchemaSet schemaSet, LinqToXsdSettings settings)
        {
            if (schemaSet == null)
            {
                throw new ArgumentNullException(nameof(schemaSet));
            }
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }
            var xsdConverter = new XsdToTypesConverter(settings);
            var mapping      = xsdConverter.GenerateMapping(schemaSet);

            var codeGenerator = new CodeDomTypesGenerator(settings);
            var ccu           = new CodeCompileUnit();

            foreach (var codeNs in codeGenerator.GenerateTypes(mapping))
            {
                ccu.Namespaces.Add(codeNs);
            }

            return(ccu);
        }
コード例 #3
0
        public static void GenerateXObjects(
            XmlSchemaSet set,
            string csFileName,
            string configFileName,
            string assemblyName,
            bool xmlSerializable,
            bool nameMangler2)
        {
            var configSettings = new LinqToXsdSettings(nameMangler2);

            if (configFileName != null)
            {
                configSettings.Load(configFileName);
            }
            configSettings.EnableServiceReference = xmlSerializable;
            var xsdConverter = new XsdToTypesConverter(configSettings);
            var mapping      = xsdConverter.GenerateMapping(set);

            var codeGenerator = new CodeDomTypesGenerator(configSettings);
            var ccu           = new CodeCompileUnit();

            foreach (var codeNs in codeGenerator.GenerateTypes(mapping))
            {
                ccu.Namespaces.Add(codeNs);
            }
            //Write to file
            var 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());
                }
            }
        }