internal static void AddSchemaImport(string ns, XmlSchema schema)
        {
            if (SchemaHelper.NamespacesEqual(ns, schema.TargetNamespace) || SchemaHelper.NamespacesEqual(ns, Globals.SchemaNamespace) || SchemaHelper.NamespacesEqual(ns, Globals.SchemaInstanceNamespace))
            {
                return;
            }

            foreach (object item in schema.Includes)
            {
                if (item is XmlSchemaImport)
                {
                    if (SchemaHelper.NamespacesEqual(ns, ((XmlSchemaImport)item).Namespace))
                    {
                        return;
                    }
                }
            }

            XmlSchemaImport import = new XmlSchemaImport();

            if (ns != null && ns.Length > 0)
            {
                import.Namespace = ns;
            }

            schema.Includes.Add(import);
        }
        private void ExportClassDataContract(ClassDataContract classDataContract, XmlSchema schema)
        {
            XmlSchemaComplexType type = new XmlSchemaComplexType
            {
                Name = classDataContract.StableName.Name
            };

            schema.Items.Add(type);
            XmlElement genericInfoElement = null;

            if (classDataContract.UnderlyingType.IsGenericType)
            {
                genericInfoElement = ExportGenericInfo(classDataContract.UnderlyingType, Globals.GenericTypeLocalName, Globals.SerializationNamespace);
            }

            XmlSchemaSequence rootSequence = new XmlSchemaSequence();

            for (int i = 0; i < classDataContract.Members.Count; i++)
            {
                DataMember dataMember = classDataContract.Members[i];

                XmlSchemaElement element = new XmlSchemaElement
                {
                    Name = dataMember.Name
                };
                XmlElement   actualTypeElement  = null;
                DataContract memberTypeContract = _dataContractSet.GetMemberTypeDataContract(dataMember);
                if (CheckIfMemberHasConflict(dataMember))
                {
                    element.SchemaTypeName = AnytypeQualifiedName;
                    actualTypeElement      = ExportActualType(memberTypeContract.StableName);
                    SchemaHelper.AddSchemaImport(memberTypeContract.StableName.Namespace, schema);
                }
                else
                {
                    SetElementType(element, memberTypeContract, schema);
                }

                SchemaHelper.AddElementForm(element, schema);
                if (dataMember.IsNullable)
                {
                    element.IsNillable = true;
                }

                if (!dataMember.IsRequired)
                {
                    element.MinOccurs = 0;
                }

                element.Annotation = GetSchemaAnnotation(actualTypeElement, ExportSurrogateData(dataMember), ExportEmitDefaultValue(dataMember));
                rootSequence.Items.Add(element);
            }

            XmlElement isValueTypeElement = null;

            if (classDataContract.BaseContract != null)
            {
                XmlSchemaComplexContentExtension extension = CreateTypeContent(type, classDataContract.BaseContract.StableName, schema);
                extension.Particle = rootSequence;
                if (classDataContract.IsReference && !classDataContract.BaseContract.IsReference)
                {
                    AddReferenceAttributes(extension.Attributes, schema);
                }
            }
            else
            {
                type.Particle = rootSequence;
                if (classDataContract.IsValueType)
                {
                    isValueTypeElement = GetAnnotationMarkup(IsValueTypeName, XmlConvert.ToString(classDataContract.IsValueType), schema);
                }

                if (classDataContract.IsReference)
                {
                    AddReferenceAttributes(type.Attributes, schema);
                }
            }
            type.Annotation = GetSchemaAnnotation(genericInfoElement, ExportSurrogateData(classDataContract), isValueTypeElement);
        }
        private void ExportCollectionDataContract(CollectionDataContract collectionDataContract, XmlSchema schema)
        {
            XmlSchemaComplexType type = new XmlSchemaComplexType
            {
                Name = collectionDataContract.StableName.Name
            };

            schema.Items.Add(type);
            XmlElement genericInfoElement = null, isDictionaryElement = null;

            if (collectionDataContract.UnderlyingType.IsGenericType && CollectionDataContract.IsCollectionDataContract(collectionDataContract.UnderlyingType))
            {
                genericInfoElement = ExportGenericInfo(collectionDataContract.UnderlyingType, Globals.GenericTypeLocalName, Globals.SerializationNamespace);
            }

            if (collectionDataContract.IsDictionary)
            {
                isDictionaryElement = ExportIsDictionary();
            }

            type.Annotation = GetSchemaAnnotation(isDictionaryElement, genericInfoElement, ExportSurrogateData(collectionDataContract));

            XmlSchemaSequence rootSequence = new XmlSchemaSequence();

            XmlSchemaElement element = new XmlSchemaElement
            {
                Name            = collectionDataContract.ItemName,
                MinOccurs       = 0,
                MaxOccursString = Globals.OccursUnbounded
            };

            if (collectionDataContract.IsDictionary)
            {
                ClassDataContract    keyValueContract = collectionDataContract.ItemContract as ClassDataContract;
                XmlSchemaComplexType keyValueType     = new XmlSchemaComplexType();
                XmlSchemaSequence    keyValueSequence = new XmlSchemaSequence();
                foreach (DataMember dataMember in keyValueContract.Members)
                {
                    XmlSchemaElement keyValueElement = new XmlSchemaElement
                    {
                        Name = dataMember.Name
                    };
                    SetElementType(keyValueElement, _dataContractSet.GetMemberTypeDataContract(dataMember), schema);
                    SchemaHelper.AddElementForm(keyValueElement, schema);
                    if (dataMember.IsNullable)
                    {
                        keyValueElement.IsNillable = true;
                    }

                    keyValueElement.Annotation = GetSchemaAnnotation(ExportSurrogateData(dataMember));
                    keyValueSequence.Items.Add(keyValueElement);
                }
                keyValueType.Particle = keyValueSequence;
                element.SchemaType    = keyValueType;
            }
            else
            {
                if (collectionDataContract.IsItemTypeNullable)
                {
                    element.IsNillable = true;
                }

                DataContract itemContract = _dataContractSet.GetItemTypeDataContract(collectionDataContract);
                SetElementType(element, itemContract, schema);
            }
            SchemaHelper.AddElementForm(element, schema);
            rootSequence.Items.Add(element);

            type.Particle = rootSequence;

            if (collectionDataContract.IsReference)
            {
                AddReferenceAttributes(type.Attributes, schema);
            }
        }
 private XmlSchema GetSchema(string ns)
 {
     return(SchemaHelper.GetSchema(ns, Schemas));
 }