void ExportMembersMapping(XmlMembersMapping xmlMembersMapping, bool exportEnclosingType) { ClassMap cmap = (ClassMap)xmlMembersMapping.ObjectMap; if (xmlMembersMapping.HasWrapperElement && exportEnclosingType) { XmlSchema schema = GetSchema(xmlMembersMapping.Namespace); XmlSchemaComplexType stype = new XmlSchemaComplexType(); XmlSchemaSequence particle; XmlSchemaAnyAttribute anyAttribute; ExportMembersMapSchema(schema, cmap, null, stype.Attributes, out particle, out anyAttribute); stype.Particle = particle; stype.AnyAttribute = anyAttribute; if (encodedFormat) { stype.Name = xmlMembersMapping.ElementName; schema.Items.Add(stype); } else { XmlSchemaElement selem = new XmlSchemaElement(); selem.Name = xmlMembersMapping.ElementName; selem.SchemaType = stype; schema.Items.Add(selem); } } else { ICollection members = cmap.ElementMembers; if (members != null) { foreach (XmlTypeMapMemberElement member in members) { if (member is XmlTypeMapMemberAnyElement && member.TypeData.IsListType) { XmlSchema mschema = GetSchema(xmlMembersMapping.Namespace); XmlSchemaParticle par = GetSchemaArrayElement(mschema, member.ElementInfo); if (par is XmlSchemaAny) { XmlSchemaComplexType ct = FindComplexType(mschema.Items, "any"); if (ct != null) { continue; } ct = new XmlSchemaComplexType(); ct.Name = "any"; ct.IsMixed = true; XmlSchemaSequence seq = new XmlSchemaSequence(); ct.Particle = seq; seq.Items.Add(par); mschema.Items.Add(ct); continue; } } XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo)member.ElementInfo [0]; XmlSchema schema; if (encodedFormat) { schema = GetSchema(xmlMembersMapping.Namespace); ImportNamespace(schema, XmlSerializer.EncodingNamespace); } else { schema = GetSchema(einfo.Namespace); } XmlSchemaElement exe = FindElement(schema.Items, einfo.ElementName); XmlSchemaElement elem; XmlSchemaObjectContainer container = null; // In encoded format, the schema elements are not needed if (!encodedFormat) { container = new XmlSchemaObjectContainer(schema); } Type memType = member.GetType(); if (member is XmlTypeMapMemberFlatList) { throw new InvalidOperationException("Unwrapped arrays not supported as parameters"); } else if (memType == typeof(XmlTypeMapMemberElement)) { elem = (XmlSchemaElement)GetSchemaElement(schema, einfo, member.DefaultValue, false, container); } else { elem = (XmlSchemaElement)GetSchemaElement(schema, einfo, false, container); } if (exe != null) { if (exe.SchemaTypeName.Equals(elem.SchemaTypeName)) { schema.Items.Remove(elem); } else { string s = "The XML element named '" + einfo.ElementName + "' "; s += "from namespace '" + schema.TargetNamespace + "' references distinct types " + elem.SchemaTypeName.Name + " and " + exe.SchemaTypeName.Name + ". "; s += "Use XML attributes to specify another XML name or namespace for the element or types."; throw new InvalidOperationException(s); } } } } } CompileSchemas(); }
XmlSchemaParticle GetSchemaElement(XmlSchema currentSchema, XmlTypeMapElementInfo einfo, object defaultValue, bool isTypeMember, XmlSchemaObjectContainer container) { if (einfo.IsTextElement) { return(null); } if (einfo.IsUnnamedAnyElement) { XmlSchemaAny any = new XmlSchemaAny(); any.MinOccurs = 0; any.MaxOccurs = 1; if (container != null) { container.Items.Add(any); } return(any); } XmlSchemaElement selem = new XmlSchemaElement(); selem.IsNillable = einfo.IsNullable; if (container != null) { container.Items.Add(selem); } if (isTypeMember) { selem.MaxOccurs = 1; selem.MinOccurs = einfo.IsNullable ? 1 : 0; if ((defaultValue == DBNull.Value && einfo.TypeData.IsValueType && einfo.Member != null && !einfo.Member.IsOptionalValueType) || encodedFormat) { selem.MinOccurs = 1; } } XmlSchema memberSchema = null; if (!encodedFormat) { memberSchema = GetSchema(einfo.Namespace); ImportNamespace(currentSchema, einfo.Namespace); } if (currentSchema == memberSchema || encodedFormat || !isTypeMember) { if (isTypeMember) { selem.IsNillable = einfo.IsNullable; } selem.Name = einfo.ElementName; if (defaultValue != System.DBNull.Value) { selem.DefaultValue = ExportDefaultValue(einfo.TypeData, einfo.MappedType, defaultValue); } if (einfo.Form != XmlSchemaForm.Qualified) { selem.Form = einfo.Form; } switch (einfo.TypeData.SchemaType) { case SchemaTypes.XmlNode: selem.SchemaType = GetSchemaXmlNodeType(); break; case SchemaTypes.XmlSerializable: SetSchemaXmlSerializableType(einfo.MappedType as XmlSerializableMapping, selem); ExportXmlSerializableSchema(currentSchema, einfo.MappedType as XmlSerializableMapping); break; case SchemaTypes.Enum: selem.SchemaTypeName = new XmlQualifiedName(einfo.MappedType.XmlType, einfo.MappedType.XmlTypeNamespace); ImportNamespace(currentSchema, einfo.MappedType.XmlTypeNamespace); ExportEnumSchema(einfo.MappedType); break; case SchemaTypes.Array: XmlQualifiedName atypeName = ExportArraySchema(einfo.MappedType, currentSchema.TargetNamespace); selem.SchemaTypeName = atypeName; ImportNamespace(currentSchema, atypeName.Namespace); break; case SchemaTypes.Class: if (einfo.MappedType.TypeData.Type != typeof(object)) { selem.SchemaTypeName = new XmlQualifiedName(einfo.MappedType.XmlType, einfo.MappedType.XmlTypeNamespace); ImportNamespace(currentSchema, einfo.MappedType.XmlTypeNamespace); } else if (encodedFormat) { selem.SchemaTypeName = new XmlQualifiedName(einfo.MappedType.XmlType, einfo.MappedType.XmlTypeNamespace); } ExportClassSchema(einfo.MappedType); break; case SchemaTypes.Primitive: selem.SchemaTypeName = new XmlQualifiedName(einfo.TypeData.XmlType, einfo.DataTypeNamespace); if (!einfo.TypeData.IsXsdType) { ImportNamespace(currentSchema, einfo.MappedType.XmlTypeNamespace); ExportDerivedSchema(einfo.MappedType); } break; } } else { selem.RefName = new XmlQualifiedName(einfo.ElementName, einfo.Namespace); foreach (XmlSchemaObject ob in memberSchema.Items) { if (ob is XmlSchemaElement && ((XmlSchemaElement)ob).Name == einfo.ElementName) { return(selem); } } GetSchemaElement(memberSchema, einfo, defaultValue, false, new XmlSchemaObjectContainer(memberSchema)); } return(selem); }
XmlSchemaParticle GetSchemaElement(XmlSchema currentSchema, XmlTypeMapElementInfo einfo, bool isTypeMember, XmlSchemaObjectContainer container) { return(GetSchemaElement(currentSchema, einfo, System.DBNull.Value, isTypeMember, container)); }
void ExportMembersMapping (XmlMembersMapping xmlMembersMapping, bool exportEnclosingType) { ClassMap cmap = (ClassMap) xmlMembersMapping.ObjectMap; if (xmlMembersMapping.HasWrapperElement && exportEnclosingType) { XmlSchema schema = GetSchema (xmlMembersMapping.Namespace); XmlSchemaComplexType stype = new XmlSchemaComplexType (); XmlSchemaSequence particle; XmlSchemaAnyAttribute anyAttribute; ExportMembersMapSchema (schema, cmap, null, stype.Attributes, out particle, out anyAttribute); stype.Particle = particle; stype.AnyAttribute = anyAttribute; if (encodedFormat) { stype.Name = xmlMembersMapping.ElementName; schema.Items.Add (stype); } else { XmlSchemaElement selem = new XmlSchemaElement (); selem.Name = xmlMembersMapping.ElementName; selem.SchemaType = stype; schema.Items.Add (selem); } } else { ICollection members = cmap.ElementMembers; if (members != null) { foreach (XmlTypeMapMemberElement member in members) { if (member is XmlTypeMapMemberAnyElement && member.TypeData.IsListType) { XmlSchema mschema = GetSchema (xmlMembersMapping.Namespace); XmlSchemaParticle par = GetSchemaArrayElement (mschema, member.ElementInfo); if (par is XmlSchemaAny) { XmlSchemaComplexType ct = FindComplexType (mschema.Items, "any"); if (ct != null) continue; ct = new XmlSchemaComplexType (); ct.Name = "any"; ct.IsMixed = true; XmlSchemaSequence seq = new XmlSchemaSequence (); ct.Particle = seq; seq.Items.Add (par); mschema.Items.Add (ct); continue; } } XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo) member.ElementInfo [0]; XmlSchema schema; if (encodedFormat) { schema = GetSchema (xmlMembersMapping.Namespace); ImportNamespace (schema, XmlSerializer.EncodingNamespace); } else schema = GetSchema (einfo.Namespace); XmlSchemaElement exe = FindElement (schema.Items, einfo.ElementName); XmlSchemaElement elem; XmlSchemaObjectContainer container = null; // In encoded format, the schema elements are not needed if (!encodedFormat) container = new XmlSchemaObjectContainer (schema); Type memType = member.GetType(); if (member is XmlTypeMapMemberFlatList) throw new InvalidOperationException ("Unwrapped arrays not supported as parameters"); else if (memType == typeof(XmlTypeMapMemberElement)) elem = (XmlSchemaElement) GetSchemaElement (schema, einfo, member.DefaultValue, false, container); else elem = (XmlSchemaElement) GetSchemaElement (schema, einfo, false, container); if (exe != null) { if (exe.SchemaTypeName.Equals (elem.SchemaTypeName)) schema.Items.Remove (elem); else { string s = "The XML element named '" + einfo.ElementName + "' "; s += "from namespace '" + schema.TargetNamespace + "' references distinct types " + elem.SchemaTypeName.Name + " and " + exe.SchemaTypeName.Name + ". "; s += "Use XML attributes to specify another XML name or namespace for the element or types."; throw new InvalidOperationException (s); } } } } } CompileSchemas (); }
XmlSchemaParticle GetSchemaElement (XmlSchema currentSchema, XmlTypeMapElementInfo einfo, object defaultValue, bool isTypeMember, XmlSchemaObjectContainer container) { if (einfo.IsTextElement) return null; if (einfo.IsUnnamedAnyElement) { XmlSchemaAny any = new XmlSchemaAny (); any.MinOccurs = 0; any.MaxOccurs = 1; if (container != null) container.Items.Add (any); return any; } XmlSchemaElement selem = new XmlSchemaElement (); selem.IsNillable = einfo.IsNullable; if (container != null) container.Items.Add (selem); if (isTypeMember) { selem.MaxOccurs = 1; selem.MinOccurs = einfo.IsNullable ? 1 : 0; if ((defaultValue == DBNull.Value && einfo.TypeData.IsValueType && einfo.Member != null && !einfo.Member.IsOptionalValueType) || encodedFormat) selem.MinOccurs = 1; } XmlSchema memberSchema = null; if (!encodedFormat) { memberSchema = GetSchema (einfo.Namespace); ImportNamespace (currentSchema, einfo.Namespace); } if (currentSchema == memberSchema || encodedFormat || !isTypeMember) { if (isTypeMember) selem.IsNillable = einfo.IsNullable; selem.Name = einfo.ElementName; if (defaultValue != System.DBNull.Value) selem.DefaultValue = ExportDefaultValue (einfo.TypeData, einfo.MappedType, defaultValue); if (einfo.Form != XmlSchemaForm.Qualified) selem.Form = einfo.Form; switch (einfo.TypeData.SchemaType) { case SchemaTypes.XmlNode: selem.SchemaType = GetSchemaXmlNodeType (); break; case SchemaTypes.XmlSerializable: SetSchemaXmlSerializableType (einfo.MappedType as XmlSerializableMapping, selem); ExportXmlSerializableSchema (currentSchema, einfo.MappedType as XmlSerializableMapping); break; case SchemaTypes.Enum: selem.SchemaTypeName = new XmlQualifiedName (einfo.MappedType.XmlType, einfo.MappedType.XmlTypeNamespace); ImportNamespace (currentSchema, einfo.MappedType.XmlTypeNamespace); ExportEnumSchema (einfo.MappedType); break; case SchemaTypes.Array: XmlQualifiedName atypeName = ExportArraySchema (einfo.MappedType, currentSchema.TargetNamespace); selem.SchemaTypeName = atypeName; ImportNamespace (currentSchema, atypeName.Namespace); break; case SchemaTypes.Class: if (einfo.MappedType.TypeData.Type != typeof(object)) { selem.SchemaTypeName = new XmlQualifiedName (einfo.MappedType.XmlType, einfo.MappedType.XmlTypeNamespace); ImportNamespace (currentSchema, einfo.MappedType.XmlTypeNamespace); } else if (encodedFormat) selem.SchemaTypeName = new XmlQualifiedName (einfo.MappedType.XmlType, einfo.MappedType.XmlTypeNamespace); ExportClassSchema (einfo.MappedType); break; case SchemaTypes.Primitive: selem.SchemaTypeName = new XmlQualifiedName (einfo.TypeData.XmlType, einfo.DataTypeNamespace); if (!einfo.TypeData.IsXsdType) { ImportNamespace (currentSchema, einfo.MappedType.XmlTypeNamespace); ExportDerivedSchema (einfo.MappedType); } break; } } else { selem.RefName = new XmlQualifiedName (einfo.ElementName, einfo.Namespace); foreach (XmlSchemaObject ob in memberSchema.Items) if (ob is XmlSchemaElement && ((XmlSchemaElement)ob).Name == einfo.ElementName) return selem; GetSchemaElement (memberSchema, einfo, defaultValue, false, new XmlSchemaObjectContainer (memberSchema)); } return selem; }
XmlSchemaParticle GetSchemaElement (XmlSchema currentSchema, XmlTypeMapElementInfo einfo, bool isTypeMember, XmlSchemaObjectContainer container) { return GetSchemaElement (currentSchema, einfo, System.DBNull.Value, isTypeMember, container); }