internal string GenerateTypedDataSet(XmlSchemaElement element, XmlSchemas schemas, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeDomProvider codeProvider)
        {
            if (element == null)
            {
                return(null);
            }
            if (this.importedTypes[element.SchemaType] != null)
            {
                return((string)this.importedTypes[element.SchemaType]);
            }
            IList list = schemas.GetSchemas(element.QualifiedName.Namespace);

            if (list.Count != 1)
            {
                return(null);
            }
            XmlSchema schema = list[0] as XmlSchema;

            if (schema == null)
            {
                return(null);
            }
            MemoryStream stream = new MemoryStream();

            schema.Write(stream);
            stream.Position = 0L;
            DesignDataSource designDS = new DesignDataSource();

            designDS.ReadXmlSchema(stream, null);
            stream.Close();
            string str = TypedDataSetGenerator.GenerateInternal(designDS, compileUnit, mainNamespace, codeProvider, this.dataSetGenerateOptions, null);

            this.importedTypes.Add(element.SchemaType, str);
            return(str);
        }
        public override string ImportSchemaType(string name, string schemaNamespace, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider)
        {
            IList list = schemas.GetSchemas(schemaNamespace);

            if (list.Count != 1)
            {
                return(null);
            }
            XmlSchema schema = list[0] as XmlSchema;

            if (schema == null)
            {
                return(null);
            }
            XmlSchemaType type = (XmlSchemaType)schema.SchemaTypes[new XmlQualifiedName(name, schemaNamespace)];

            return(this.ImportSchemaType(type, context, schemas, importer, compileUnit, mainNamespace, options, codeProvider));
        }
예제 #3
0
 /// <summary>
 /// Get Schema for a given body
 /// </summary>
 /// <param name="body"></param>
 /// <param name="isXmlSerializerType"></param>
 /// <returns></returns>
 private static Message GetXmlSchemaAsMessage(Type body, bool isXmlSerializerType)
 {
     System.Collections.IEnumerable schemas;
     if (isXmlSerializerType)
     {
         XmlReflectionImporter importer    = new XmlReflectionImporter();
         XmlTypeMapping        typeMapping = importer.ImportTypeMapping(body);
         XmlSchemas            s           = new XmlSchemas();
         XmlSchemaExporter     exporter    = new XmlSchemaExporter(s);
         exporter.ExportTypeMapping(typeMapping);
         schemas = s.GetSchemas(null);
     }
     else
     {
         XsdDataContractExporter exporter = new XsdDataContractExporter();
         exporter.Export(body);
         schemas = exporter.Schemas.Schemas();
     }
     using (MemoryStream stream = new MemoryStream())
     {
         XmlWriterSettings xws = new XmlWriterSettings {
             Indent = true
         };
         using (XmlWriter writer = XmlWriter.Create(stream, xws))
         {
             if (writer != null)
             {
                 writer.WriteStartElement("Schemas");
                 foreach (XmlSchema schema in schemas)
                 {
                     if (schema.TargetNamespace != "http://www.w3.org/2001/XMLSchema")
                     {
                         schema.Write(writer);
                     }
                 }
             }
         }
         stream.Seek(0, SeekOrigin.Begin);
         using (XmlReader reader = XmlReader.Create(stream))
         {
             return(Message.CreateMessage(MessageVersion.None, null, XElement.Load(reader, LoadOptions.PreserveWhitespace)));
         }
     }
 }
예제 #4
0
 private void AddImport(XmlSchema schema, Hashtable imports)
 {
     if (schema == null || imports[schema] != null)
     {
         return;
     }
     imports.Add(schema, schema);
     foreach (XmlSchemaExternal external in schema.Includes)
     {
         if (external is XmlSchemaImport)
         {
             XmlSchemaImport import = (XmlSchemaImport)external;
             foreach (XmlSchema s in allSchemas.GetSchemas(import.Namespace))
             {
                 AddImport(s, imports);
             }
         }
     }
 }
        internal string GenerateTypedDataSet(XmlSchemaElement element, XmlSchemas schemas, CodeNamespace codeNamespace, StringCollection references, CodeDomProvider codeProvider)
        {
            if (element == null)
            {
                return(null);
            }
            if (this.importedTypes[element.SchemaType] != null)
            {
                return((string)this.importedTypes[element.SchemaType]);
            }
            IList list = schemas.GetSchemas(element.QualifiedName.Namespace);

            if (list.Count != 1)
            {
                return(null);
            }
            XmlSchema schema = list[0] as XmlSchema;

            if (schema == null)
            {
                return(null);
            }
            DataSet dataSet = new DataSet();

            using (MemoryStream stream = new MemoryStream())
            {
                schema.Write(stream);
                stream.Position = 0L;
                dataSet.ReadXmlSchema(stream);
            }
            string name = new TypedDataSetGenerator().GenerateCode(dataSet, codeNamespace, codeProvider.CreateGenerator()).Name;

            this.importedTypes.Add(element.SchemaType, name);
            references.Add("System.Data.dll");
            return(name);
        }
 public override string ImportSchemaType(XmlSchemaType type, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider)
 {
     if (type != null)
     {
         if (this.importedTypes[type] != null)
         {
             mainNamespace.Imports.Add(new CodeNamespaceImport(typeof(DataSet).Namespace));
             compileUnit.ReferencedAssemblies.Add("System.Data.dll");
             return((string)this.importedTypes[type]);
         }
         if (!(context is XmlSchemaElement))
         {
             return(null);
         }
         XmlSchemaElement element1 = (XmlSchemaElement)context;
         if (type is XmlSchemaComplexType)
         {
             XmlSchemaComplexType type2 = (XmlSchemaComplexType)type;
             if (type2.Particle is XmlSchemaSequence)
             {
                 XmlSchemaObjectCollection items = ((XmlSchemaSequence)type2.Particle).Items;
                 if (((2 == items.Count) && (items[0] is XmlSchemaAny)) && (items[1] is XmlSchemaAny))
                 {
                     XmlSchemaAny any2 = (XmlSchemaAny)items[0];
                     XmlSchemaAny any3 = (XmlSchemaAny)items[1];
                     if ((any2.Namespace == "http://www.w3.org/2001/XMLSchema") && (any3.Namespace == "urn:schemas-microsoft-com:xml-diffgram-v1"))
                     {
                         string ns = null;
                         foreach (XmlSchemaAttribute attribute in type2.Attributes)
                         {
                             if (attribute.Name == "namespace")
                             {
                                 ns = attribute.FixedValue.Trim();
                                 break;
                             }
                         }
                         bool flag = false;
                         if (((XmlSchemaSequence)type2.Particle).MaxOccurs == 79228162514264337593543950335M)
                         {
                             flag = true;
                         }
                         else if (any2.MaxOccurs == 79228162514264337593543950335M)
                         {
                             flag = false;
                         }
                         else
                         {
                             return(null);
                         }
                         if (ns == null)
                         {
                             string str4 = flag ? typeof(DataSet).FullName : typeof(DataTable).FullName;
                             this.importedTypes.Add(type, str4);
                             mainNamespace.Imports.Add(new CodeNamespaceImport(typeof(DataSet).Namespace));
                             compileUnit.ReferencedAssemblies.Add("System.Data.dll");
                             return(str4);
                         }
                         foreach (XmlSchema schema2 in schemas.GetSchemas(ns))
                         {
                             if ((schema2 != null) && (schema2.Id != null))
                             {
                                 XmlSchemaElement element2 = this.FindDataSetElement(schema2);
                                 if (element2 != null)
                                 {
                                     return(this.ImportSchemaType(element2.SchemaType, element2, schemas, importer, compileUnit, mainNamespace, options, codeProvider));
                                 }
                             }
                         }
                         return(null);
                     }
                 }
             }
             if ((type2.Particle is XmlSchemaSequence) || (type2.Particle is XmlSchemaAll))
             {
                 XmlSchemaObjectCollection objects = ((XmlSchemaGroupBase)type2.Particle).Items;
                 if (objects.Count == 2)
                 {
                     if (!(objects[0] is XmlSchemaElement) || !(objects[1] is XmlSchemaAny))
                     {
                         return(null);
                     }
                     XmlSchemaElement element3 = (XmlSchemaElement)objects[0];
                     if ((element3.RefName.Name != "schema") || (element3.RefName.Namespace != "http://www.w3.org/2001/XMLSchema"))
                     {
                         return(null);
                     }
                     string fullName = typeof(DataSet).FullName;
                     this.importedTypes.Add(type, fullName);
                     mainNamespace.Imports.Add(new CodeNamespaceImport(typeof(DataSet).Namespace));
                     compileUnit.ReferencedAssemblies.Add("System.Data.dll");
                     return(fullName);
                 }
                 if (1 == objects.Count)
                 {
                     XmlSchemaAny any = objects[0] as XmlSchemaAny;
                     if (((any != null) && (any.Namespace != null)) && (any.Namespace.IndexOfAny(new char[] { '#', ' ' }) < 0))
                     {
                         foreach (XmlSchema schema in schemas.GetSchemas(any.Namespace))
                         {
                             if ((schema != null) && (schema.Id != null))
                             {
                                 XmlSchemaElement element = this.FindDataSetElement(schema);
                                 if (element != null)
                                 {
                                     return(this.ImportSchemaType(element.SchemaType, element, schemas, importer, compileUnit, mainNamespace, options, codeProvider));
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return(null);
 }
예제 #7
0
        private CodeCompileUnit CreateCodeNamespace(XmlSchemaSet schemaSet, CodeNamespaceProvider namespaceProvider)
        {
            var compileUnit = new CodeCompileUnit();
            var schemas     = new XmlSchemas();

            foreach (var s in schemaSet.Schemas())
            {
                schemas.Add((XmlSchema)s);
            }

            schemas.Compile(_options.ValidationHandler, true);

            foreach (XmlSchema schema in schemas)
            {
                var codeNamespace = namespaceProvider.CreateCodeNamespace(schema);
                compileUnit.Namespaces.Add(codeNamespace);
            }

            var codeOptions = CodeGenerationOptions.GenerateOrder | CodeGenerationOptions.GenerateNewAsync;

            // foreach (var ns in rootNamespaces.Concat(extensionNamespaces))
            foreach (XmlSchema schema in schemas)
            {
                var schemaImporter = new XmlSchemaImporter(schemas);
                schemaImporter.Extensions.Clear();

                // var schema = schemas[ns];
                var codeNamespace = compileUnit.Namespaces.OfType <CodeNamespace>().Single(x => x.UserData[CodeTypeMemberExtensions.XmlSchemaKey] == schema);

                Console.WriteLine($"Import root namespace: {schema.TargetNamespace}");

                var codeExporter = new XmlCodeExporter(codeNamespace, compileUnit, codeOptions);

                foreach (XmlSchemaElement element in schema.Elements.Values.OfType <XmlSchemaElement>())
                {
                    Console.WriteLine($"Import type mapping for {element.QualifiedName.Namespace} : {element.QualifiedName.Name}");
                    var xmlTypeMapping = schemaImporter.ImportTypeMapping(element.QualifiedName);
                    codeExporter.ExportTypeMapping(xmlTypeMapping);
                }

                var baseTypes = schema.Elements.Values.OfType <XmlSchemaElement>().Select(x => x.SchemaTypeName.Name).ToList();

                foreach (XmlSchemaComplexType element in schema.SchemaTypes.Values.OfType <XmlSchemaComplexType>().Where(x => !baseTypes.Contains(x.Name)))
                {
                    Console.WriteLine($"Import schema type for {element.QualifiedName.Namespace} : {element.QualifiedName.Name}");

                    var xmlTypeMapping = schemaImporter.ImportSchemaType(element.QualifiedName);
                    codeExporter.ExportTypeMapping(xmlTypeMapping);
                }
            }

            var codeDeclarations = compileUnit.Namespaces.OfType <CodeNamespace>().SelectMany(x => x.Types.Cast <CodeTypeDeclaration>()).ToList();

            foreach (var codeDecl in codeDeclarations)
            {
                codeDecl.Comments.Clear();
                foreach (var item in codeDecl.Members.OfType <CodeTypeMember>())
                {
                    item.Comments.Clear();
                }

                var qname = codeDecl.GetQualifiedName();
                codeDecl.UserData[CodeTypeMemberExtensions.QualifiedNameKey] = qname;
                // Note, this can fail when there are multiple schemas for one namespace, like urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2
                var schema = schemas.GetSchemas(qname.Namespace).OfType <XmlSchema>().FirstOrDefault();
                // var schema = schemas[qname.Namespace];
                codeDecl.UserData[CodeTypeMemberExtensions.XmlSchemaKey] = schema;
                var xmlSchemaType = (XmlSchemaType)schema.SchemaTypes[qname];
                codeDecl.UserData[CodeTypeMemberExtensions.XmlSchemaTypeKey] = xmlSchemaType;

                foreach (CodeTypeMember member in codeDecl.Members)
                {
                    member.UserData[CodeTypeMemberExtensions.XmlSchemaKey]     = schema;
                    member.UserData[CodeTypeMemberExtensions.XmlSchemaTypeKey] = xmlSchemaType;
                }
            }

            return(compileUnit);
        }
 public override string ImportSchemaType(XmlSchemaType type, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider)
 {
     if (type != null)
     {
         if (!(context is XmlSchemaElement))
         {
             return(null);
         }
         XmlSchemaElement e = (XmlSchemaElement)context;
         if (IsDataSet(e))
         {
             if (this.importedTypes[type] != null)
             {
                 return((string)this.importedTypes[type]);
             }
             return(this.GenerateTypedDataSet(e, schemas, compileUnit, mainNamespace, codeProvider));
         }
         if (type is XmlSchemaComplexType)
         {
             XmlSchemaComplexType type2 = (XmlSchemaComplexType)type;
             if (type2.Particle is XmlSchemaSequence)
             {
                 XmlSchemaObjectCollection items = ((XmlSchemaSequence)type2.Particle).Items;
                 if (((items.Count == 2) && (items[0] is XmlSchemaAny)) && (items[1] is XmlSchemaAny))
                 {
                     XmlSchemaAny any  = (XmlSchemaAny)items[0];
                     XmlSchemaAny any2 = (XmlSchemaAny)items[1];
                     if ((any.Namespace == "http://www.w3.org/2001/XMLSchema") && (any2.Namespace == "urn:schemas-microsoft-com:xml-diffgram-v1"))
                     {
                         string ns   = null;
                         string str2 = null;
                         foreach (XmlSchemaAttribute attribute in type2.Attributes)
                         {
                             if (attribute.Name == "namespace")
                             {
                                 ns = attribute.FixedValue.Trim();
                             }
                             else if (attribute.Name == "tableTypeName")
                             {
                                 str2 = attribute.FixedValue.Trim();
                             }
                             if ((ns != null) && (str2 != null))
                             {
                                 break;
                             }
                         }
                         if (ns == null)
                         {
                             return(null);
                         }
                         IList list = schemas.GetSchemas(ns);
                         if (list.Count != 1)
                         {
                             return(null);
                         }
                         XmlSchema schema = list[0] as XmlSchema;
                         if ((schema == null) || (schema.Id == null))
                         {
                             return(null);
                         }
                         XmlSchemaElement element2 = this.FindDataSetElement(schema, schemas);
                         if (element2 == null)
                         {
                             return(null);
                         }
                         string str3 = this.ImportSchemaType(element2.SchemaType, element2, schemas, importer, compileUnit, mainNamespace, options, codeProvider);
                         if (str2 == null)
                         {
                             return(str3);
                         }
                         return(CodeGenHelper.GetTypeName(codeProvider, str3, str2));
                     }
                 }
             }
             if ((type2.Particle is XmlSchemaSequence) || (type2.Particle is XmlSchemaAll))
             {
                 XmlSchemaObjectCollection objects2 = ((XmlSchemaGroupBase)type2.Particle).Items;
                 if (objects2.Count == 1)
                 {
                     if (objects2[0] is XmlSchemaAny)
                     {
                         XmlSchemaAny any3 = (XmlSchemaAny)objects2[0];
                         if (any3.Namespace == null)
                         {
                             return(null);
                         }
                         if (any3.Namespace.IndexOf('#') >= 0)
                         {
                             return(null);
                         }
                         if (any3.Namespace.IndexOf(' ') >= 0)
                         {
                             return(null);
                         }
                         IList list2 = schemas.GetSchemas(any3.Namespace);
                         if (list2.Count != 1)
                         {
                             return(null);
                         }
                         XmlSchema schema2 = list2[0] as XmlSchema;
                         if (schema2 == null)
                         {
                             return(null);
                         }
                         if (schema2.Id == null)
                         {
                             return(null);
                         }
                         XmlSchemaElement element3 = this.FindDataSetElement(schema2, schemas);
                         if (element3 != null)
                         {
                             return(this.ImportSchemaType(element3.SchemaType, element3, schemas, importer, compileUnit, mainNamespace, options, codeProvider));
                         }
                     }
                     return(null);
                 }
             }
         }
     }
     return(null);
 }