public override string ImportSchemaType(XmlSchemaType type, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider) {
			if (type == null) {
				return null;
			}

			if (importedTypes[type] != null) {
				mainNamespace.Imports.Add(new CodeNamespaceImport(typeof(DataSet).Namespace));
				compileUnit.ReferencedAssemblies.Add("System.Data.dll");
				return (string)importedTypes[type];
			}
			if (!(context is XmlSchemaElement))
				return null;

			if (type is XmlSchemaComplexType) {
				XmlSchemaComplexType ct = (XmlSchemaComplexType)type;
				if (ct.Particle is XmlSchemaSequence) {
					XmlSchemaObjectCollection items = ((XmlSchemaSequence)ct.Particle).Items;
					if (items.Count == 2 && items[0] is XmlSchemaAny && items[1] is XmlSchemaAny) {
						XmlSchemaAny any0 = (XmlSchemaAny)items[0];
						XmlSchemaAny any1 = (XmlSchemaAny)items[1];
						if (any0.Namespace == XmlSchema.Namespace && any1.Namespace == "urn:schemas-microsoft-com:xml-diffgram-v1") {
							string typeName = typeof(DataTable).FullName;
							importedTypes.Add(type, typeName);
							mainNamespace.Imports.Add(new CodeNamespaceImport(typeof(DataTable).Namespace));
							compileUnit.ReferencedAssemblies.Add("System.Data.dll");
							return typeName;
						}
					}
				}
			}
			return null;
		}
Ejemplo n.º 2
0
        public override string ImportSchemaType(
            string name,
            string ns,
            XmlSchemaObject context,
            XmlSchemas schemas,
            XmlSchemaImporter importer,
            CodeCompileUnit compileUnit,
            CodeNamespace mainNamespace,
            CodeGenerationOptions options,
            CodeDomProvider codeProvider)
        {
            if (ns != "http://www.w3.org/2001/XMLSchema")
                return null;

            switch (name)
            {
                case "anyURI":     return "System.Uri"; 
                case "gDay":       return "System.Runtime.Remoting.Metadata.W3cXsd2001.SoapDay";
                case "gMonth":     return "System.Runtime.Remoting.Metadata.W3cXsd2001.SoapMonth";
                case "gMonthDay":  return "System.Runtime.Remoting.Metadata.W3cXsd2001.SoapMonthDay";
                case "gYear":      return "System.Runtime.Remoting.Metadata.W3cXsd2001.SoapYear"; 
                case "gYearMonth": return "System.Runtime.Remoting.Metadata.W3cXsd2001.SoapYearMonth";
                case "duration":   return "System.Runtime.Remoting.Metadata.W3cXsd2001.SoapDuration"; 
                default: return null;
            }
        }
Ejemplo n.º 3
0
 public static CodeNamespace Process(string xsdFile,
     string targetNamespace)
 {
     // Load the XmlSchema and its collection.
     XmlSchema xsd;
     using (FileStream fs = new FileStream("Untitled1.xsd", FileMode.Open))
     {
         xsd = XmlSchema.Read(fs, null);
         xsd.Compile(null);
     }
     XmlSchemas schemas = new XmlSchemas();
     schemas.Add(xsd);
     // Create the importer for these schemas.
     XmlSchemaImporter importer = new XmlSchemaImporter(schemas);
     // System.CodeDom namespace for the XmlCodeExporter to put classes in.
     CodeNamespace ns = new CodeNamespace(targetNamespace);
     XmlCodeExporter exporter = new XmlCodeExporter(ns);
     // Iterate schema top-level elements and export code for each.
     foreach (XmlSchemaElement element in xsd.Elements.Values)
     {
         // Import the mapping first.
         XmlTypeMapping mapping = importer.ImportTypeMapping(
           element.QualifiedName);
         // Export the code finally.
         exporter.ExportTypeMapping(mapping);
     }
     return ns;
 }
        public override string ImportSchemaType(
            string name, 
            string ns,
            XmlSchemaObject context, 
            XmlSchemas schemas,
            XmlSchemaImporter importer,
            CodeCompileUnit compileUnit, 
            CodeNamespace codeNamespace,
            CodeGenerationOptions options, 
            CodeDomProvider codeGenerator)
        {

            if (IsBaseType(name, ns))
            {
                return base.ImportSchemaType(name, ns,
                context, schemas,
                importer,
                compileUnit, codeNamespace,
                options, codeGenerator);
            }

            // Add the Namespace, except the first
            for (int i = 1; i < ImportNamespaces.Length; i++)
            {
                string _Import = ImportNamespaces[i];
                codeNamespace.Imports.Add(new CodeNamespaceImport(_Import));
            }

            return name;
        }
Ejemplo n.º 5
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;
        }
        //public override string ImportSchemaType(
        //    string name, 
        //    string ns, 
        //    XmlSchemaObject context, 
        //    XmlSchemas schemas, 
        //    XmlSchemaImporter importer,
        //    CodeCompileUnit compileUnit, 
        //    CodeNamespace mainNamespace, 
        //    CodeGenerationOptions options, 
        //    CodeDomProvider codeProvider)
        //{

        //    XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType) schemas.Find(new XmlQualifiedName(name, ns), typeof(XmlSchemaSimpleType));
        //    return ImportSchemaType(
        //        simpleType, 
        //        context, 
        //        schemas, 
        //        importer, 
        //        compileUnit, 
        //        mainNamespace, 
        //        options, 
        //        codeProvider);
        //}

        public override string ImportSchemaType(
            XmlSchemaType type, 
            XmlSchemaObject context, 
            XmlSchemas schemas, 
            XmlSchemaImporter importer,
            CodeCompileUnit compileUnit, 
            CodeNamespace mainNamespace, 
            CodeGenerationOptions options, 
            CodeDomProvider codeProvider)
        {

            XmlSchemaAnnotated annotatedType  = type as XmlSchemaAnnotated;
            if (annotatedType == null)
                return null;

            if (annotatedType.Annotation == null)
                return null;

            // create the comments and add them to the hash table under the namespace of the object
            CreateComments(annotatedType);

            //mainNamespace.Types.

            return null;

        }
 public override string ImportSchemaType(string name, string xmlNamespace, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider)
 {
     if ((this.m_direct && (context is XmlSchemaElement)) && ((string.CompareOrdinal(this.m_name, name) == 0) && (string.CompareOrdinal(this.m_targetNamespace, xmlNamespace) == 0)))
     {
         compileUnit.ReferencedAssemblies.AddRange(this.m_references);
         mainNamespace.Imports.AddRange(this.m_namespaceImports);
         return this.m_destinationType;
     }
     return null;
 }
		public override string ImportSchemaType(string name, string schemaNamespace, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider) {
			IList values = schemas.GetSchemas(schemaNamespace);
			if (values.Count != 1) {
				return null;
			}
			XmlSchema schema = values[0] as XmlSchema;
			if (schema == null)
				return null;
			XmlSchemaType type = (XmlSchemaType)schema.SchemaTypes[new XmlQualifiedName(name, schemaNamespace)];
			return ImportSchemaType(type, context, schemas, importer, compileUnit, mainNamespace, options, codeProvider);
		}
		public override string ImportSchemaType (XmlSchemaType type, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider)
		{
			if (type == null)
				return null;

			var xe = context as XmlSchemaElement;
			if (xe == null)
				return null;

			return null;
		}
Ejemplo n.º 10
0
		public override string ImportSchemaType (string name, string ns, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider)
		{
			if (ns == "http://monkeywrench.novell.com/") {
				if (name != "ArrayOfString" && name != "ArrayOfInt1") {
					mainNamespace.Imports.Add (new CodeNamespaceImport ("MonkeyWrench.DataClasses"));
					mainNamespace.Imports.Add (new CodeNamespaceImport ("MonkeyWrench.DataClasses.Logic"));
					return name;
				}
			}

			return base.ImportSchemaType (name, ns, context, schemas, importer, compileUnit, mainNamespace, options, codeProvider);
		}
        private static void Main(string[] args)
        {
            XmlSchema rootSchema = GetSchemaFromFile("fpml-main-4-2.xsd");

            var schemaSet = new List<XmlSchemaExternal>();

            ExtractIncludes(rootSchema, ref schemaSet);

            var schemas = new XmlSchemas { rootSchema };

            schemaSet.ForEach(schemaExternal => schemas.Add(GetSchemaFromFile(schemaExternal.SchemaLocation)));

            schemas.Compile(null, true);

            var xmlSchemaImporter = new XmlSchemaImporter(schemas);

            var codeNamespace = new CodeNamespace("Hosca.FpML4_2");
            var xmlCodeExporter = new XmlCodeExporter(codeNamespace);

            var xmlTypeMappings = new List<XmlTypeMapping>();

            foreach (XmlSchemaType schemaType in rootSchema.SchemaTypes.Values)
                xmlTypeMappings.Add(xmlSchemaImporter.ImportSchemaType(schemaType.QualifiedName));
            foreach (XmlSchemaElement schemaElement in rootSchema.Elements.Values)
                xmlTypeMappings.Add(xmlSchemaImporter.ImportTypeMapping(schemaElement.QualifiedName));

            xmlTypeMappings.ForEach(xmlCodeExporter.ExportTypeMapping);

            CodeGenerator.ValidateIdentifiers(codeNamespace);

            foreach (CodeTypeDeclaration codeTypeDeclaration in codeNamespace.Types)
            {
                for (int i = codeTypeDeclaration.CustomAttributes.Count - 1; i >= 0; i--)
                {
                    CodeAttributeDeclaration cad = codeTypeDeclaration.CustomAttributes[i];
                    if (cad.Name == "System.CodeDom.Compiler.GeneratedCodeAttribute")
                        codeTypeDeclaration.CustomAttributes.RemoveAt(i);
                }
            }

            using (var writer = new StringWriter())
            {
                new CSharpCodeProvider().GenerateCodeFromNamespace(codeNamespace, writer, new CodeGeneratorOptions());

                //Console.WriteLine(writer.GetStringBuilder().ToString());

                File.WriteAllText(Path.Combine(rootFolder, "FpML4_2.Generated.cs"), writer.GetStringBuilder().ToString());
            }

            Console.ReadLine();
        }
 public override string ImportSchemaType(XmlSchemaType type, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider)
 {
     if ((!this.m_direct && (type is XmlSchemaSimpleType)) && (context is XmlSchemaElement))
     {
         XmlQualifiedName qualifiedName = ((XmlSchemaSimpleType) type).BaseXmlSchemaType.QualifiedName;
         if ((string.CompareOrdinal(this.m_name, qualifiedName.Name) == 0) && (string.CompareOrdinal(this.m_targetNamespace, qualifiedName.Namespace) == 0))
         {
             compileUnit.ReferencedAssemblies.AddRange(this.m_references);
             mainNamespace.Imports.AddRange(this.m_namespaceImports);
             return this.m_destinationType;
         }
     }
     return null;
 }
 public override string ImportSchemaType(string name, string ns, XmlSchemaObject context, XmlSchemas schemas,
     XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider)
 {
     if (XmlSchema.Namespace == ns)
     {
         switch (name)
         {
             case "dateTime":
                 string codeTypeName = typeof(DateTimeOffset).FullName;
                 return codeTypeName;
             default: return null;
         }
     }
     else { return null; }
 }
Ejemplo n.º 14
0
		[Category ("NotWorking")] // mark it NotWorking until fixes have landed in svn
		public void ImportTypeMapping_XsdPrimitive_AnyType ()
		{
			XmlSchemas schemas = ExportType (typeof (object));
			ArrayList qnames = GetXmlQualifiedNames (schemas);
			Assert.AreEqual (1, qnames.Count, "#1");

			XmlSchemaImporter importer = new XmlSchemaImporter (schemas);
			XmlTypeMapping map = importer.ImportTypeMapping ((XmlQualifiedName) qnames[0]);

			Assert.IsNotNull (map, "#2");
			Assert.AreEqual ("anyType", map.ElementName, "#3");
			Assert.AreEqual ("NSObject", map.Namespace, "#4");
			Assert.AreEqual ("System.Object", map.TypeFullName, "#5");
			Assert.AreEqual ("Object", map.TypeName, "#6");
		}
Ejemplo n.º 15
0
		public void ImportTypeMapping_Struct ()
		{
			XmlSchemas schemas = ExportType (typeof (TimeSpan));
			ArrayList qnames = GetXmlQualifiedNames (schemas);
			Assert.AreEqual (1, qnames.Count, "#1");

			XmlSchemaImporter importer = new XmlSchemaImporter (schemas);
			XmlTypeMapping map = importer.ImportTypeMapping ((XmlQualifiedName) qnames[0]);

			Assert.IsNotNull (map, "#2");
			Assert.AreEqual ("TimeSpan", map.ElementName, "#3");
			Assert.AreEqual ("NSTimeSpan", map.Namespace, "#4");
			Assert.AreEqual ("TimeSpan", map.TypeFullName, "#5");
			Assert.AreEqual ("TimeSpan", map.TypeName, "#6");
		}
Ejemplo n.º 16
0
        public override string ImportSchemaType(
            XmlSchemaType type, 
            XmlSchemaObject context, 
            XmlSchemas schemas, 
            XmlSchemaImporter importer,
            CodeCompileUnit compileUnit, 
            CodeNamespace mainNamespace, 
            CodeGenerationOptions options, 
            CodeDomProvider codeProvider)
        {

            XmlSchemaSimpleType simpleType = type as XmlSchemaSimpleType;
            if (simpleType == null)
                return null;

            string typeName = null;
            if (generatedTypes.TryGetValue(simpleType, out typeName))
                return typeName;

            XmlSchemaSimpleTypeRestriction restriction = simpleType.Content as XmlSchemaSimpleTypeRestriction;
            if (restriction == null)
                return null;

            // genetate type only for xs:string restrictions
            if (restriction.BaseTypeName.Name != "string" || restriction.BaseTypeName.Namespace != XmlSchema.Namespace)
                return null;

            // does not generate custom type if it has any enumeration facets
            foreach (object o in restriction.Facets)
            {
                if (o is XmlSchemaEnumerationFacet)
                    return null;
            }

            typeName = GenerateSimpleType(simpleType, compileUnit, mainNamespace, options, codeProvider);

            // add generated type information to the cache to avoid generating type al the time
            if (typeName != null)
            {
                generatedTypes[simpleType] = typeName;
                clrTypes.Add(typeName, typeName);
            }

            return typeName;
        }
Ejemplo n.º 17
0
		public override string ImportSchemaType(string name, string ns, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider)
		{
			// Uncomment these lines for debugging.
			// (Messages are displayed when you run wsdl.exe
			// and this schema importer is called.)
			//Console.WriteLine("ImportSchemaType");
			//Console.WriteLine(name);
			//Console.WriteLine(ns);
			//Console.WriteLine();

			if (name.Equals("FileData") &&
				ns.Equals("http://www.apress.com/ProASP.NET/FileData"))
			{
				mainNamespace.Imports.Add(new CodeNamespaceImport("FileDataComponent"));
				return "FileData";
			}
			return null;
		}
Ejemplo n.º 18
0
        public static void GenerateCode(string xsdFilepath, string codeOutPath, string nameSpace)
        {
            FileStream stream = File.OpenRead(xsdFilepath);
            XmlSchema xsd = XmlSchema.Read(stream, ValidationCallbackOne);

            // Remove namespaceattribute from schema
            xsd.TargetNamespace = null;

            XmlSchemas xsds = new XmlSchemas { xsd };
            XmlSchemaImporter imp = new XmlSchemaImporter(xsds);
            //imp.Extensions.Add(new CustomSchemaImporterExtension());

            CodeNamespace ns = new CodeNamespace(nameSpace);
            XmlCodeExporter exp = new XmlCodeExporter(ns);

            foreach (XmlSchemaObject item in xsd.Items)
            {
                if (!(item is XmlSchemaElement))
                    continue;

                XmlSchemaElement xmlSchemaElement = (XmlSchemaElement)item;
                XmlQualifiedName xmlQualifiedName = new XmlQualifiedName(xmlSchemaElement.Name, xsd.TargetNamespace);
                XmlTypeMapping map = imp.ImportTypeMapping(xmlQualifiedName);

                exp.ExportTypeMapping(map);
            }

            // Remove all the attributes from each type in the CodeNamespace, except
            // System.Xml.Serialization.XmlTypeAttribute
            RemoveAttributes(ns);

            ToProperties(ns);

            CodeCompileUnit compileUnit = new CodeCompileUnit();

            compileUnit.Namespaces.Add(ns);
            CSharpCodeProvider provider = new CSharpCodeProvider();

            using (StreamWriter sw = new StreamWriter(codeOutPath, false))
            {
                CodeGeneratorOptions codeGeneratorOptions = new CodeGeneratorOptions();
                provider.GenerateCodeFromCompileUnit(compileUnit, sw, codeGeneratorOptions);
            }
        }
 public override string ImportSchemaType(string name, string ns, XmlSchemaObject context,
     XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit,
     CodeNamespace mainNamespace, CodeGenerationOptions options,
     CodeDomProvider codeProvider)
 {
     if (discoveredTypes == null)
         DiscoverTypes();
     string key = ns + name;
     Type type;
     if (!discoveredTypes.TryGetValue(key, out type))
         return base.ImportSchemaType(name, ns, context, schemas, importer, compileUnit,
             mainNamespace, options, codeProvider);
     compileUnit.ReferencedAssemblies.Add(type.Assembly.FullName);
     XmlSchemaElement schemaElement = context as XmlSchemaElement;
     if ((schemaElement != null) && schemaElement.IsNillable &&
         (schemaElement.ElementSchemaType is XmlSchemaSimpleType))
         return String.Format("System.Nullable<{0}>", type.FullName);
     return type.FullName;
 }
Ejemplo n.º 20
0
        private static void GenerateClasses(CodeNamespace code, XmlSchema schema)
        {
            XmlSchemas schemas = new XmlSchemas();
            schemas.Add(schema);
            XmlSchemaImporter importer = new XmlSchemaImporter(schemas);

            CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
            CodeGenerationOptions options = CodeGenerationOptions.None;
            XmlCodeExporter exporter = new XmlCodeExporter(code, codeCompileUnit, options);

            foreach (XmlSchemaObject item in schema.Items)
            {
                XmlSchemaElement element = item as XmlSchemaElement;

                if (element != null)
                {
                    XmlQualifiedName name = new XmlQualifiedName(element.Name, schema.TargetNamespace);
                    XmlTypeMapping map = importer.ImportTypeMapping(name);
                    exporter.ExportTypeMapping(map);
                }
            }
        }
Ejemplo n.º 21
0
        public override string ImportSchemaType(
            string name, 
            string ns, 
            XmlSchemaObject context, 
            XmlSchemas schemas, 
            XmlSchemaImporter importer,
            CodeCompileUnit compileUnit, 
            CodeNamespace mainNamespace, 
            CodeGenerationOptions options, 
            CodeDomProvider codeProvider)
        {

            XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType) schemas.Find(new XmlQualifiedName(name, ns), typeof(XmlSchemaSimpleType));
            return ImportSchemaType(
                simpleType, 
                context, 
                schemas, 
                importer, 
                compileUnit, 
                mainNamespace, 
                options, 
                codeProvider);
        }
Ejemplo n.º 22
0
        private CodeNamespace GeneratedClassFromStream(Stream stream, string nameSpace)
        {
            XmlSchema xsd;
            stream.Seek(0, SeekOrigin.Begin);
            using (stream)
            {

                xsd = XmlSchema.Read(stream, null);
            }

            XmlSchemas xsds = new XmlSchemas();

            xsds.Add(xsd);
            xsds.Compile(null, true);
            XmlSchemaImporter schemaImporter = new XmlSchemaImporter(xsds);

            // create the codedom
            CodeNamespace codeNamespace = new CodeNamespace(nameSpace);
            XmlCodeExporter codeExporter = new XmlCodeExporter(codeNamespace);
            List<XmlTypeMapping> maps = new List<XmlTypeMapping>();
            foreach (XmlSchemaType schemaType in xsd.SchemaTypes.Values)
            {
                maps.Add(schemaImporter.ImportSchemaType(schemaType.QualifiedName));
            }
            foreach (XmlSchemaElement schemaElement in xsd.Elements.Values)
            {
                maps.Add(schemaImporter.ImportTypeMapping(schemaElement.QualifiedName));
            }
            foreach (XmlTypeMapping map in maps)
            {
                codeExporter.ExportTypeMapping(map);
            }

            this.RemoveUnusedStuff(codeNamespace);

            return codeNamespace;
        }
        public void Run(string src, string dest)
        {
            // Load the schema to process.
            XmlSchema xsd;
            using (Stream stm = File.OpenRead(src))
                xsd = XmlSchema.Read(stm, null);
            // Collection of schemas for the XmlSchemaImporter
            XmlSchemas xsds = new XmlSchemas();
            xsds.Add(xsd);
            XmlSchemaImporter imp = new XmlSchemaImporter(xsds);

            // System.CodeDom namespace for the XmlCodeExporter to put classes in
            CodeNamespace ns = new CodeNamespace("NHibernate.Mapping.Hbm");
            CodeCompileUnit ccu =  new CodeCompileUnit();
            XmlCodeExporter exp = new XmlCodeExporter(ns, ccu, ~CodeGenerationOptions.GenerateProperties);

            // Iterate schema items (top-level elements only) and generate code for each
            foreach (XmlSchemaObject item in xsd.Items)
            {
                if (item is XmlSchemaElement)
                {
                    // Import the mapping first
                    XmlTypeMapping map = imp.ImportTypeMapping(new XmlQualifiedName(((XmlSchemaElement)item).Name, xsd.TargetNamespace));
                    // Export the code finally
                    exp.ExportTypeMapping(map);
                }
            }
            ns.Imports.Add(new CodeNamespaceImport("Ayende.NHibernateQueryAnalyzer.SchemaEditing"));
            AddRequiredTags(ns, xsd);

            // Code generator to build code with.
            CodeDomProvider generator = new CSharpCodeProvider();

            // Generate untouched version
            using (StreamWriter sw = new StreamWriter(dest, false))
                generator.GenerateCodeFromNamespace(ns, sw, new CodeGeneratorOptions());
        }
        public static CodeNamespace Process(string xsdSchema, string targetNamespace)
        {
            // Load the XmlSchema and its collection.
            XmlSchema xsd;
            using (var fs = new StringReader(xsdSchema))
            {
                xsd = XmlSchema.Read(fs, null);
                xsd.Compile(null);
            }
            XmlSchemas schemas = new XmlSchemas();
            schemas.Add(xsd);
            // Create the importer for these schemas.
            XmlSchemaImporter importer = new XmlSchemaImporter(schemas);
            // System.CodeDom namespace for the XmlCodeExporter to put classes in.
            CodeNamespace ns = new CodeNamespace(targetNamespace);
            XmlCodeExporter exporter = new XmlCodeExporter(ns);
            // Iterate schema top-level elements and export code for each.
            foreach (XmlSchemaElement element in xsd.Elements.Values)
            {
                // Import the mapping first.
                XmlTypeMapping mapping = importer.ImportTypeMapping(
                    element.QualifiedName);
                // Export the code finally.
                exporter.ExportTypeMapping(mapping);
            }

            // execute extensions

            //var collectionsExt = new ArraysToCollectionsExtension();
            //collectionsExt.Process(ns, xsd);

            //var filedsExt = new FieldsToPropertiesExtension();
            //filedsExt.Process(ns, xsd);

            return ns;
        }
 public virtual string ImportSchemaType(string name, string ns, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider)
 {
     return null;
 }
 public virtual string ImportAnyElement(XmlSchemaAny any, bool mixed, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider)
 {
     return null;
 }
		protected override void BeginNamespace ()
		{
			xmlImporter = new XmlSchemaImporter (LiteralSchemas, ClassNames);
			soapImporter = new SoapSchemaImporter (EncodedSchemas, ClassNames);
			xmlExporter = new XmlCodeExporter (CodeNamespace, null);
			xmlReflectionImporter = new XmlReflectionImporter ();
		}
		protected override void BeginNamespace ()
		{
#if NET_2_0
			xmlImporter = new XmlSchemaImporter (LiteralSchemas, base.CodeGenerationOptions, base.CodeGenerator, base.ImportContext);
			soapImporter = new SoapSchemaImporter (EncodedSchemas, base.CodeGenerationOptions, base.CodeGenerator, base.ImportContext);
			xmlExporter = new XmlCodeExporter (CodeNamespace, null, base.CodeGenerator, base.CodeGenerationOptions, null);
			soapExporter = new SoapCodeExporter (CodeNamespace, null, base.CodeGenerator, base.CodeGenerationOptions, null);
#else
			xmlImporter = new XmlSchemaImporter (LiteralSchemas, ClassNames);
			soapImporter = new SoapSchemaImporter (EncodedSchemas, ClassNames);
			xmlExporter = new XmlCodeExporter (CodeNamespace, null);
			soapExporter = new SoapCodeExporter (CodeNamespace, null);
#endif
		}
		public void ExportSimpleContentExtensionEnum ()
		{
			string xsd = @"
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:b='urn:bar' targetNamespace='urn:bar'>
  <xs:element name='Foo' type='b:DayOfWeek' />
  <xs:complexType name='DayOfWeek'>
    <xs:simpleContent>
      <xs:extension base='b:WeekDay' />
    </xs:simpleContent>
  </xs:complexType>
  <xs:simpleType name='WeekDay'>
    <xs:restriction base='xs:string'>
      <xs:enumeration value='Sunday'/>
      <xs:enumeration value='Monday'/>
      <xs:enumeration value='Tuesday'/>
      <xs:enumeration value='Wednesday'/>
      <xs:enumeration value='Thursday'/>
      <xs:enumeration value='Friday'/>
      <xs:enumeration value='Saturday'/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>";
			XmlSchema xs = XmlSchema.Read (new StringReader (xsd), null);
			XmlSchemas xss = new XmlSchemas ();
			xss.Add (xs);
			XmlSchemaImporter imp = new XmlSchemaImporter (xss);
			XmlTypeMapping m = imp.ImportTypeMapping (new XmlQualifiedName ("Foo", "urn:bar"));
			CodeNamespace cns = new CodeNamespace ();
			XmlCodeExporter exp = new XmlCodeExporter (cns);
			exp.ExportTypeMapping (m);
			CodeTypeDeclaration enumType = null;
			foreach (CodeTypeDeclaration ctd in cns.Types)
				if (ctd.Name == "WeekDay")
					enumType = ctd;
			Assert.IsNotNull (enumType);
		}
		public void DuplicateIdentifiers ()
		{
			XmlSchema xs = XmlSchema.Read (File.OpenText ("Test/XmlFiles/xsd/82078.xsd"), null);

			XmlSchemas xss = new XmlSchemas ();
			xss.Add (xs);
			XmlSchemaImporter imp = new XmlSchemaImporter (xss);
			CodeNamespace cns = new CodeNamespace ();
			XmlCodeExporter exp = new XmlCodeExporter (cns);
			XmlQualifiedName qname = new XmlQualifiedName (
				"Operation", "http://tempuri.org/");
			exp.ExportTypeMapping (imp.ImportTypeMapping (qname));
			CodeCompileUnit ccu = new CodeCompileUnit ();
			ccu.Namespaces.Add (cns);

			CodeDomProvider provider = new CSharpCodeProvider ();
			ICodeCompiler compiler = provider.CreateCompiler ();

			CompilerParameters options = new CompilerParameters ();
			options.ReferencedAssemblies.Add ("System.dll");
			options.ReferencedAssemblies.Add ("System.Xml.dll");
			options.GenerateInMemory = true;

			CompilerResults result = compiler.CompileAssemblyFromDom (options, ccu);
			Assert.AreEqual (0, result.Errors.Count, "#1");
			Assert.IsNotNull (result.CompiledAssembly, "#2");
		}