Esempio n. 1
0
        public static CodeNamespace GenerateCode(Stream schemaStream, string classesNamespace)
        {
            // Open schema
            XmlSchema schema = XmlSchema.Read(schemaStream, null);
            XmlSchemas schemas = new XmlSchemas();
            schemas.Add(schema);
            schemas.Compile(null, true);

            // Generate code
            CodeNamespace code = new CodeNamespace(classesNamespace);
            XmlSchemaImporter importer = new XmlSchemaImporter(schemas);
            XmlCodeExporter exporter = new XmlCodeExporter(code);
            foreach (XmlSchemaElement element in schema.Elements.Values) {
                XmlTypeMapping mapping = importer.ImportTypeMapping(element.QualifiedName);
                exporter.ExportTypeMapping(mapping);
            }

            // Modify generated code using extensions
            schemaStream.Position = 0; // Rewind stream to the start
            XPathDocument xPathDoc = new XPathDocument(schemaStream);
            CodeGenerationContext context = new CodeGenerationContext(code, schema, xPathDoc);

            new ExplicitXmlNamesExtension().ApplyTo(context);
            new DocumentationExtension().ApplyTo(context);
            new FixXmlTextAttributeExtension().ApplyTo(context);
            new ArraysToGenericExtension().ApplyTo(context);
            new CamelCaseExtension().ApplyTo(context);
            new GetByIDExtension().ApplyTo(context);

            return code;
        }
Esempio n. 2
0
        public virtual void ApplyTo(CodeGenerationContext context)
        {
            this.context = context;

            Begin();

            ArrayList originalTypes = new ArrayList(this.Code.Types);

            foreach (CodeTypeDeclaration type in originalTypes) {
                if (type.IsClass || type.IsStruct) {
                    ArrayList originalMembers = new ArrayList(type.Members);
                    foreach (CodeTypeMember member in originalMembers) {
                        CodeMemberProperty property = member as CodeMemberProperty;
                        if (property == null) continue;

                        CodeMemberField field = GetFieldForProperty(type, property);
                        ProcessProperty(type, field, property);
                    }
                }
            }

            End();
        }