private void myLoadXsdButton_Click(object sender, EventArgs e) { if (myOpenSchemaDialog.ShowDialog(this) != DialogResult.OK) { return; } try { using (XmlReader reader = XmlReader.Create(myOpenSchemaDialog.FileName)) { m_schema = XmlSchema.Read(reader, new ValidationEventHandler(myValidationEventHandler)); XmlSchemaSet set = new XmlSchemaSet(); set.ValidationEventHandler += new ValidationEventHandler(myValidationEventHandler); set.Add(m_schema); set.Compile(); } mySchemaFileNameTextBox.Text = myOpenSchemaDialog.FileName; } catch (Exception ex) { MessageBox.Show(ex.ToString()); return; } try { XsdClassGenerator generator = new XsdClassGenerator(m_schema); generator.SchemaImporterExtension.Add(new CodeGeneration.ImporterExtensions.SimpleTypeExtension()); generator.Compile(); myAssemblyTreeView.Assembly = generator.Assembly; myCodeRichTextBox.Text = generator.CodeString; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private void myLoadXsdButton_Click(object sender, EventArgs e) { if (myOpenSchemaDialog.ShowDialog(this) != DialogResult.OK) return; try { using (XmlReader reader = XmlReader.Create(myOpenSchemaDialog.FileName)) { m_schema = XmlSchema.Read(reader, new ValidationEventHandler(myValidationEventHandler)); XmlSchemaSet set = new XmlSchemaSet(); set.ValidationEventHandler += new ValidationEventHandler(myValidationEventHandler); set.Add(m_schema); set.Compile(); } mySchemaFileNameTextBox.Text = myOpenSchemaDialog.FileName; } catch (Exception ex) { MessageBox.Show(ex.ToString()); return; } try { XsdClassGenerator generator = new XsdClassGenerator(m_schema); generator.SchemaImporterExtension.Add(new CodeGeneration.ImporterExtensions.SimpleTypeExtension()); generator.Compile(); myAssemblyTreeView.Assembly = generator.Assembly; myCodeRichTextBox.Text = generator.CodeString; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
/// <summary> /// Generates the output. /// </summary> protected override string OnGenerateCode(string inputFileName, string inputFileContent) { #region load the configuration Configuration.Load(base.InputFilePath); StringBuilder output = new StringBuilder(); output.Append(CustomTool.GetToolGeneratedCodeWarning(typeof(XsdCodeGenerator))); #endregion #region read the schema(s) XmlSchema xsd; Directory.SetCurrentDirectory(Path.GetDirectoryName(base.InputFilePath)); using (FileStream fs = File.OpenRead(base.InputFilePath)) { xsd = XmlSchema.Read(fs, null); } #endregion #region create the class generator and set default options XsdClassGenerator xsdClassGenerator = new XsdClassGenerator(xsd); xsdClassGenerator.CodeGenerationOptions = CodeGenerationOptions.None; if (Configuration.Current.EnableDataBinding) { xsdClassGenerator.CodeGenerationOptions |= CodeGenerationOptions.EnableDataBinding; } if (Configuration.Current.GenerateOrder) { xsdClassGenerator.CodeGenerationOptions |= CodeGenerationOptions.GenerateOrder; } if (Configuration.Current.GenerateProperties) { xsdClassGenerator.CodeGenerationOptions |= CodeGenerationOptions.GenerateProperties; } xsdClassGenerator.CodeNamespaceString = base.FileNameSpace; xsdClassGenerator.CodeGeneratorOptions.GenerateCodeString = true; xsdClassGenerator.CodeGeneratorOptions.GenerateObjects = false; xsdClassGenerator.CodeGeneratorOptions.CompileAssembly = false; #endregion #region load the code modifiers foreach (AssemblyType assembly in Configuration.Current.CodeModifiers) { ObjectHandle handle = Activator.CreateInstance(assembly.Assembly, assembly.Type); if (handle == null) { output.AppendFormat("//\tWarning, could not create CodeModifier type {0} from assembly {0}", assembly.Type, assembly.Assembly); continue; } ICodeModifier modifier = handle.Unwrap() as ICodeModifier; if (modifier == null) { output.AppendFormat("//\tWarning CodeModifier {0} from assembly {0} does not derive from ICodeModifier", assembly.Type, assembly.Assembly); continue; } modifier.XmlOptions = assembly.Any; xsdClassGenerator.CodeModifiers.Add(modifier); } #endregion #region load the SchemaImporterExtensions foreach (AssemblyType assembly in Configuration.Current.SchemaImporterExtensions) { ObjectHandle handle = Activator.CreateInstance(assembly.Assembly, assembly.Type); if (handle == null) { output.AppendFormat("//\tWarning, could not create SchemaImporterExtensions type {0} from assembly {0}", assembly.Type, assembly.Assembly); continue; } SchemaImporterExtension extension = handle.Unwrap() as SchemaImporterExtension; if (extension == null) { output.AppendFormat("//\tWarning SchemaImporterExtensions {0} from assembly {0} does not derive from SchemaImporterExtension", assembly.Type, assembly.Assembly); continue; } xsdClassGenerator.SchemaImporterExtensions.Add(extension); } #endregion // generate code xsdClassGenerator.Compile(); // save config file and make sure it's added to the project Configuration.Save(base.InputFilePath); AddToProject(Configuration.GetConfigFileName(base.InputFilePath)); #region Workaround for known bug with fixed attributes: output.AppendLine(Resources.Message_1591); output.AppendLine(Resources.Pragma_1591_Disable); output.Append(xsdClassGenerator.CodeString); output.Append(Resources.Pragma_1591_Enable); #endregion return(output.ToString()); }