System.Xml.Serialization.XmlSchemaExporter.ExportSpecialMapping C# (CSharp) Method

ExportSpecialMapping() private method

private ExportSpecialMapping ( SpecialMapping mapping, string ns, bool isAny, XmlSchemaElement element ) : XmlSchemaType
mapping SpecialMapping
ns string
isAny bool
element System.Xml.Schema.XmlSchemaElement
return System.Xml.Schema.XmlSchemaType
        private XmlSchemaType ExportSpecialMapping(SpecialMapping mapping, string ns, bool isAny, XmlSchemaElement element)
        {
            switch (mapping.TypeDesc.Kind)
            {
                case TypeKind.Node:
                    {
                        XmlSchemaComplexType type = new XmlSchemaComplexType();
                        type.IsMixed = mapping.TypeDesc.IsMixed;
                        XmlSchemaSequence seq = new XmlSchemaSequence();
                        XmlSchemaAny any = new XmlSchemaAny();
                        if (isAny)
                        {
                            type.AnyAttribute = new XmlSchemaAnyAttribute();
                            type.IsMixed = true;
                            any.MaxOccurs = decimal.MaxValue;
                        }
                        seq.Items.Add(any);
                        type.Particle = seq;
                        if (element != null)
                            element.SchemaType = type;
                        return type;
                    }
                case TypeKind.Serializable:
                    {
                        SerializableMapping serializableMapping = (SerializableMapping)mapping;
                        if (serializableMapping.IsAny)
                        {
                            XmlSchemaComplexType type = new XmlSchemaComplexType();
                            type.IsMixed = mapping.TypeDesc.IsMixed;
                            XmlSchemaSequence seq = new XmlSchemaSequence();
                            XmlSchemaAny any = new XmlSchemaAny();
                            if (isAny)
                            {
                                type.AnyAttribute = new XmlSchemaAnyAttribute();
                                type.IsMixed = true;
                                any.MaxOccurs = decimal.MaxValue;
                            }
                            if (serializableMapping.NamespaceList.Length > 0)
                                any.Namespace = serializableMapping.NamespaceList;
                            any.ProcessContents = XmlSchemaContentProcessing.Lax;

                            if (serializableMapping.Schemas != null)
                            {
                                foreach (XmlSchema schema in serializableMapping.Schemas.Schemas())
                                {
                                    if (schema.TargetNamespace != XmlSchema.Namespace)
                                    {
                                        _schemas.Add(schema, true);
                                        AddSchemaImport(schema.TargetNamespace, ns);
                                    }
                                }
                            }
                            seq.Items.Add(any);
                            type.Particle = seq;
                            if (element != null)
                                element.SchemaType = type;
                            return type;
                        }
                        else if (serializableMapping.XsiType != null || serializableMapping.XsdType != null)
                        {
                            XmlSchemaType type = serializableMapping.XsdType;
                            // for performance reasons we need to postpone merging of the serializable schemas
                            foreach (XmlSchema schema in serializableMapping.Schemas.Schemas())
                            {
                                if (schema.TargetNamespace != XmlSchema.Namespace)
                                {
                                    _schemas.Add(schema, true);
                                    AddSchemaImport(schema.TargetNamespace, ns);
                                    if (!serializableMapping.XsiType.IsEmpty && serializableMapping.XsiType.Namespace == schema.TargetNamespace)
                                        type = (XmlSchemaType)schema.SchemaTypes[serializableMapping.XsiType];
                                }
                            }
                            if (element != null)
                            {
                                element.SchemaTypeName = serializableMapping.XsiType;
                                if (element.SchemaTypeName.IsEmpty)
                                    element.SchemaType = type;
                            }
                            // check for duplicate top-level elements XmlAttributes
                            serializableMapping.CheckDuplicateElement(element, ns);
                            return type;
                        }
                        else if (serializableMapping.Schema != null)
                        {
                            // this is the strongly-typed DataSet
                            XmlSchemaComplexType type = new XmlSchemaComplexType();
                            XmlSchemaAny any = new XmlSchemaAny();
                            XmlSchemaSequence seq = new XmlSchemaSequence();
                            seq.Items.Add(any);
                            type.Particle = seq;
                            string anyNs = serializableMapping.Schema.TargetNamespace;
                            any.Namespace = anyNs == null ? "" : anyNs;
                            XmlSchema existingSchema = _schemas[anyNs];
                            if (existingSchema == null)
                            {
                                _schemas.Add(serializableMapping.Schema);
                            }
                            else if (existingSchema != serializableMapping.Schema)
                            {
                                throw new InvalidOperationException(SR.Format(SR.XmlDuplicateNamespace, anyNs));
                            }
                            if (element != null)
                                element.SchemaType = type;

                            // check for duplicate top-level elements XmlAttributes
                            serializableMapping.CheckDuplicateElement(element, ns);
                            return type;
                        }
                        else
                        {
                            // DataSet
                            XmlSchemaComplexType type = new XmlSchemaComplexType();
                            XmlSchemaElement schemaElement = new XmlSchemaElement();
                            schemaElement.RefName = new XmlQualifiedName("schema", XmlSchema.Namespace);
                            XmlSchemaSequence seq = new XmlSchemaSequence();
                            seq.Items.Add(schemaElement);
                            seq.Items.Add(new XmlSchemaAny());
                            type.Particle = seq;
                            AddSchemaImport(XmlSchema.Namespace, ns);
                            if (element != null)
                                element.SchemaType = type;
                            return type;
                        }
                    }
                default:
                    throw new ArgumentException(SR.XmlInternalError, nameof(mapping));
            }
        }