System.Xml.Serialization.XmlSchemaImporter.ImportSchemaType C# (CSharp) Method

ImportSchemaType() public method

public ImportSchemaType ( XmlQualifiedName typeName, Type baseType, bool baseTypeCanBeIndirect ) : XmlTypeMapping
typeName System.Xml.XmlQualifiedName
baseType Type
baseTypeCanBeIndirect bool
return XmlTypeMapping
        public XmlTypeMapping ImportSchemaType(XmlQualifiedName typeName, Type baseType, bool baseTypeCanBeIndirect)
        {
            TypeMapping typeMapping = ImportType(typeName, typeof(TypeMapping), baseType, TypeFlags.CanBeElementValue, true);
            typeMapping.ReferencedByElement = false;

            ElementAccessor accessor = new ElementAccessor();
            accessor.IsTopLevelInSchema = true; // false
            accessor.Name = typeName.Name;
            accessor.Namespace = typeName.Namespace;
            accessor.Mapping = typeMapping;

            if (typeMapping is SpecialMapping && ((SpecialMapping)typeMapping).NamedAny)
                accessor.Any = true;
            accessor.IsNullable = typeMapping.TypeDesc.IsNullable;
            accessor.Form = XmlSchemaForm.Qualified;

            if (accessor.Mapping is StructMapping)
            {
                MakeDerived((StructMapping)accessor.Mapping, baseType, baseTypeCanBeIndirect);
            }
            else if (baseType != null)
            {
                if (accessor.Mapping is ArrayMapping)
                {
                    // in the case of the ArrayMapping we can use the top-level StructMapping, because it does not have base base type
                    accessor.Mapping = ((ArrayMapping)accessor.Mapping).TopLevelMapping;
                    MakeDerived((StructMapping)accessor.Mapping, baseType, baseTypeCanBeIndirect);
                }
                else
                {
                    // Type '{0}' from namespace '{1}' is not a complex type and cannot be used as a {2}.
                    throw new InvalidOperationException(SR.Format(SR.XmlBadBaseType, typeName.Name, typeName.Namespace, baseType.FullName));
                }
            }
            return new XmlTypeMapping(Scope, accessor);
        }

Same methods

XmlSchemaImporter::ImportSchemaType ( XmlQualifiedName typeName ) : XmlTypeMapping
XmlSchemaImporter::ImportSchemaType ( XmlQualifiedName typeName, Type baseType ) : XmlTypeMapping

Usage Example

        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();
        }
All Usage Examples Of System.Xml.Serialization.XmlSchemaImporter::ImportSchemaType