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;
 }
 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(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);
		}
        internal string GenerateTypedDataSet(XmlSchemaElement element, XmlSchemas schemas, CodeNamespace codeNamespace, StringCollection references, CodeDomProvider codeProvider) {
            if (element == null)
                return null;

            if (importedTypes[element.SchemaType] != null)
                return (string)importedTypes[element.SchemaType];

            IList values = schemas.GetSchemas(element.QualifiedName.Namespace);
            if (values.Count != 1) {
                return null;
            }
            XmlSchema schema = values[0] as XmlSchema;
            if (schema == null)
                return null;

            DataSet ds = new DataSet();

            // 
            using (MemoryStream stream = new MemoryStream()) {
                schema.Write(stream);
                stream.Position = 0;
                ds.ReadXmlSchema(stream);
            }

#pragma warning disable 618 // ignore obsolete warning about TypedDataSetGenerator
            CodeTypeDeclaration dsClass = new TypedDataSetGenerator().GenerateCode(ds, codeNamespace, codeProvider.CreateGenerator());
#pragma warning restore 618
            string typeName = dsClass.Name;
            importedTypes.Add(element.SchemaType, typeName);
            references.Add("System.Data.dll");
            return typeName;
        }
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        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;

            XmlSchemaElement e = (XmlSchemaElement)context;

            // recognizing the following is important, but not as part of SQLPT 120015394: Support WSI Compliant WSDL for DataSet
            // <xs:element name="NewDataSet" msdata:IsDataSet="true">
            // see also SQLBU 338644, 410965, 423446
            //if (IsDataSet(e))
            //{
            //    return GenerateTypedDataSet(e, schemas, mainNamespace, compileUnit.ReferencedAssemblies, codeProvider);
            //}

            if (type is XmlSchemaComplexType) {
                XmlSchemaComplexType ct = (XmlSchemaComplexType)type;

                if (ct.Particle is XmlSchemaSequence)
                {
                    XmlSchemaObjectCollection items = ((XmlSchemaSequence)ct.Particle).Items;
                    if ((2 == items.Count) && (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"))
                        {   // new diffgramm format
                            string ns = null;
                            foreach (XmlSchemaAttribute a in ct.Attributes)
                            {
                                if (a.Name == "namespace")
                                {
                                    ns = a.FixedValue.Trim();
                                    break;
                                }
                            }
                            bool isDataSet = false;

                            // check for DataSet or DataTable
                            if (((XmlSchemaSequence)ct.Particle).MaxOccurs == Decimal.MaxValue)
                            {
                                isDataSet = true;
                            }
                            else if (any0.MaxOccurs == Decimal.MaxValue)
                            {
                                isDataSet = false;
                            }
                            else
                            {
                                return null;
                            }

                            if (ns == null)
                            {   //Un-Typed DataSet / DataTable
                                string typeName = isDataSet ? typeof(DataSet).FullName : typeof(DataTable).FullName;
                                importedTypes.Add(type, typeName);
                                mainNamespace.Imports.Add(new CodeNamespaceImport(typeof(DataSet).Namespace));
                                compileUnit.ReferencedAssemblies.Add("System.Data.dll");
                                return typeName;
                            }
                            else
                            {   // Typed DataSet / DataTable
                                foreach (XmlSchema schema in schemas.GetSchemas(ns))
                                {
                                    if ((schema != null) && (schema.Id != null))
                                    {
                                        XmlSchemaElement ds = FindDataSetElement(schema); // implement  FindDataTableElement(schema)
                                        if (ds != null)
                                        {
                                            return ImportSchemaType(ds.SchemaType, ds, schemas, importer, compileUnit, mainNamespace, options, codeProvider);
                                        }
                                        // else return null
                                    }
                                }
                                return null;
                            }
                        }
                    }
                }
                if (ct.Particle is XmlSchemaSequence || ct.Particle is XmlSchemaAll) {
                    XmlSchemaObjectCollection items = ((XmlSchemaGroupBase)ct.Particle).Items;
                    if (items.Count == 2) {
                        if (!(items[0] is XmlSchemaElement && items[1] is XmlSchemaAny)) return null;
                        XmlSchemaElement schema = (XmlSchemaElement)items[0];
                        if (!(schema.RefName.Name == "schema" && schema.RefName.Namespace == XmlSchema.Namespace)) return null;
                        string typeName = typeof(DataSet).FullName;
                        importedTypes.Add(type, typeName);
                        mainNamespace.Imports.Add(new CodeNamespaceImport(typeof(DataSet).Namespace));
                        compileUnit.ReferencedAssemblies.Add("System.Data.dll");
                        return typeName;
                    }
                    else if (1 == items.Count)
                    {
                        XmlSchemaAny any = items[0] as XmlSchemaAny;
                        if ((null != any) &&
                            (null != any.Namespace) &&
                            (any.Namespace.IndexOfAny(new char[] { '#', ' ' }) < 0)) // special syntax (##any, ##other, ...) or more than one Uri present
                        {
                            foreach (XmlSchema schema in schemas.GetSchemas(any.Namespace))
                            {
                                if ((null != schema) &&
                                    (null != schema.Id))
                                {
                                    XmlSchemaElement ds = FindDataSetElement(schema);
                                    if (ds != null)
                                    {
                                        return ImportSchemaType(ds.SchemaType, ds, schemas, importer, compileUnit, mainNamespace, options, codeProvider);
                                    }
                                    // else return null
                                }
                                // else return null
                            }
                        }
                        // else return null
                    }
                }
            }
            return null;
        }
Example #6
0
 Message CreateSchema(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 w = XmlWriter.Create(stream, xws))
         {
             w.WriteStartElement("Schemas");
             foreach (XmlSchema schema in schemas)
             {
                 if (schema.TargetNamespace != "http://www.w3.org/2001/XMLSchema")
                 {
                     schema.Write(w);
                 }
             }
         }
         stream.Seek(0, SeekOrigin.Begin);
         using (XmlReader reader = XmlReader.Create(stream))
         {
             return Message.CreateMessage(MessageVersion.None, null, XElement.Load(reader, LoadOptions.PreserveWhitespace));
         }
     }
 }
 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;
 }
 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;
 }