/// <summary>
        /// Converts this instance to a legacy <see cref="LinqToXsdSettings"/> instance.
        /// </summary>
        /// <returns></returns>
        public LinqToXsdSettings ToLinqToXsdSettings()
        {
            var settings = new LinqToXsdSettings();

            settings.Load(new XDocument(Untyped));
            return(settings);
        }
        /// <summary>
        /// Creates a new instance of <see cref="LinqToXsdSettings"/>, optionally by loading from an XML file.
        /// </summary>
        /// <param name="fromXmlFile">Null or empty value will simply return a default instance.</param>
        /// <returns></returns>
        public static LinqToXsdSettings LoadLinqToXsdSettings(string fromXmlFile = null)
        {
            var settings = new LinqToXsdSettings();

            if (fromXmlFile.IsNotEmpty())
            {
                settings.Load(fromXmlFile);
            }

            return(settings);
        }
Esempio n. 3
0
        /// <summary>
        /// Generates code using a given <paramref name="schemaSet"/> of XSD's and a <see cref="LinqToXsdSettings"/> instance.
        /// </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 TextWriter Generate(XmlSchemaSet schemaSet, LinqToXsdSettings settings)
        {
            var ccu = GenerateCodeCompileUnit(schemaSet, settings);

            var stringWriter = new StringWriter();

            var provider             = new CSharpCodeProvider();
            var codeGeneratorOptions = new CodeGeneratorOptions();

            provider.GenerateCodeFromCompileUnit(ccu, stringWriter, codeGeneratorOptions);

            return(stringWriter);
        }
        /// <summary>
        /// Generates code using a given <paramref name="schemaSet"/> of XSD's and a <see cref="LinqToXsdSettings"/> instance.
        /// </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 TextWriter Generate(XmlSchemaSet schemaSet, LinqToXsdSettings settings)
        {
            var ccu    = GenerateCodeCompileUnit(schemaSet, settings);
            var writer = ccu.ToStringWriter();

            if (settings.NullableReferences)
            {
                // HACK: CodeDom doesn't allow us to add #pragmas.
                // In <auto-generated> code, CS mandates a "#nullable enable" pragma.
                // So we add the pragma inside the generated text directly
                writer.InsertFilePragma("#nullable enable annotations");
            }

            return(writer);
        }
Esempio n. 5
0
        /// <summary>
        /// Generates code using a given <see cref="xsdFilePath"/> for a single file, and an optional <see cref="LinqToXsdSettings"/> instance.
        /// </summary>
        /// <param name="xsdFilePath"></param>
        /// <param name="settings">If null, uses default or </param>
        /// <returns></returns>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="xsdFilePath"/> is <see langword="null"/></exception>
        public static KeyValuePair <string, TextWriter> Generate(string xsdFilePath, LinqToXsdSettings settings = null)
        {
            if (xsdFilePath.IsEmpty())
            {
                throw new ArgumentNullException(nameof(xsdFilePath));
            }
            if (settings == null)
            {
                settings = new LinqToXsdSettings();
            }

            var xmlReader = XmlReader.Create(xsdFilePath, new XmlReaderSettings {
                DtdProcessing = DtdProcessing.Parse
            });

            var schemaSet = xmlReader.ToXmlSchemaSet();

            var codeWriter = Generate(schemaSet, settings);

            return(new KeyValuePair <string, TextWriter>(xsdFilePath, codeWriter));
        }
Esempio n. 6
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());
                }
            }
        }
        /// <summary>
        /// Generates code for a sequence of file paths and an instance of a <see cref="LinqToXsdSettings"/>.
        /// </summary>
        /// <param name="xsdFilePaths"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public static Dictionary <string, TextWriter> Generate(IEnumerable <string> xsdFilePaths, LinqToXsdSettings settings)
        {
            if (xsdFilePaths == null)
            {
                throw new ArgumentNullException(nameof(xsdFilePaths));
            }

            var textWriters = xsdFilePaths.Select(file => Generate(file, settings));

            return(textWriters.ToDictionary(kvp => kvp.Key, kvp => kvp.Value));
        }
        /// <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();
            var namespaces    = codeGenerator.GenerateTypes(mapping);

            foreach (var codeNs in namespaces)
            {
                ccu.Namespaces.Add(codeNs);
            }

            return(ccu);
        }
Esempio n. 9
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());
                }
            };
        }
        /// <summary>
        /// Generates code using a given <paramref name="schemaSet"/> of XSD's and a <see cref="LinqToXsdSettings"/> instance.
        /// </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 TextWriter Generate(XmlSchemaSet schemaSet, LinqToXsdSettings settings)
        {
            var ccu = GenerateCodeCompileUnit(schemaSet, settings);

            return(ccu.ToStringWriter());
        }