internal XmlMembersMapping (string elementName, string ns, bool hasWrapperElement, bool writeAccessors, XmlMemberMapping[] mapping) : base (elementName, ns) { _hasWrapperElement = hasWrapperElement; _mapping = mapping; ClassMap map = new ClassMap (); map.IgnoreMemberNamespace = writeAccessors; foreach (XmlMemberMapping mm in mapping) map.AddMember (mm.TypeMapMember); ObjectMap = map; }
void SetListMembersDefaults(ClassMap map, object ob, bool isValueList) { if (map.ListMembers != null) { ArrayList members = map.ListMembers; for (int n = 0; n < members.Count; n++) { XmlTypeMapMember mem = (XmlTypeMapMember)members[n]; if (IsReadOnly(mem, mem.TypeData, ob, isValueList)) { continue; } if (GetMemberValue(mem, ob, isValueList) == null) { SetMemberValue(mem, ob, InitializeList(mem.TypeData), isValueList); } } } }
void ExportClassSchema(XmlTypeMapping map) { if (IsMapExported(map)) { return; } SetMapExported(map); if (map.TypeData.Type == typeof(object)) { foreach (XmlTypeMapping dmap in map.DerivedTypes) { if (dmap.TypeData.SchemaType == SchemaTypes.Class) { ExportClassSchema(dmap); } } return; } XmlSchema schema = GetSchema(map.XmlTypeNamespace); XmlSchemaComplexType stype = new XmlSchemaComplexType(); stype.Name = map.XmlType; schema.Items.Add(stype); ClassMap cmap = (ClassMap)map.ObjectMap; if (cmap.HasSimpleContent) { XmlSchemaSimpleContent simple = new XmlSchemaSimpleContent(); stype.ContentModel = simple; XmlSchemaSimpleContentExtension ext = new XmlSchemaSimpleContentExtension(); simple.Content = ext; XmlSchemaSequence particle; XmlSchemaAnyAttribute anyAttribute; ExportMembersMapSchema(schema, cmap, map.BaseMap, ext.Attributes, out particle, out anyAttribute); ext.AnyAttribute = anyAttribute; if (map.BaseMap == null) { ext.BaseTypeName = cmap.SimpleContentBaseType; } else { ext.BaseTypeName = new XmlQualifiedName(map.BaseMap.XmlType, map.BaseMap.XmlTypeNamespace); ImportNamespace(schema, map.BaseMap.XmlTypeNamespace); ExportClassSchema(map.BaseMap); } } else if (map.BaseMap != null && map.BaseMap.IncludeInSchema) { XmlSchemaComplexContent cstype = new XmlSchemaComplexContent(); XmlSchemaComplexContentExtension ext = new XmlSchemaComplexContentExtension(); ext.BaseTypeName = new XmlQualifiedName(map.BaseMap.XmlType, map.BaseMap.XmlTypeNamespace); cstype.Content = ext; stype.ContentModel = cstype; XmlSchemaSequence particle; XmlSchemaAnyAttribute anyAttribute; ExportMembersMapSchema(schema, cmap, map.BaseMap, ext.Attributes, out particle, out anyAttribute); ext.Particle = particle; ext.AnyAttribute = anyAttribute; stype.IsMixed = HasMixedContent(map); cstype.IsMixed = BaseHasMixedContent(map); ImportNamespace(schema, map.BaseMap.XmlTypeNamespace); ExportClassSchema(map.BaseMap); } else { XmlSchemaSequence particle; XmlSchemaAnyAttribute anyAttribute; ExportMembersMapSchema(schema, cmap, map.BaseMap, stype.Attributes, out particle, out anyAttribute); stype.Particle = particle; stype.AnyAttribute = anyAttribute; stype.IsMixed = cmap.XmlTextCollector != null; } foreach (XmlTypeMapping dmap in map.DerivedTypes) { if (dmap.TypeData.SchemaType == SchemaTypes.Class) { ExportClassSchema(dmap); } } }
void WriteAttributeMembers (ClassMap map, object ob, bool isValueList) { // Write attributes XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember; if (anyAttrMember != null && MemberHasValue (anyAttrMember, ob, isValueList)) { ICollection extraAtts = (ICollection) GetMemberValue (anyAttrMember, ob, isValueList); if (extraAtts != null) { foreach (XmlAttribute attr in extraAtts) if (attr.NamespaceURI != xmlNamespace) WriteXmlAttribute (attr, ob); } } ICollection attributes = map.AttributeMembers; if (attributes != null) { foreach (XmlTypeMapMemberAttribute attr in attributes) { if (MemberHasValue (attr, ob, isValueList)) WriteAttribute (attr.AttributeName, attr.Namespace, GetStringValue (attr.MappedType, attr.TypeData, GetMemberValue (attr, ob, isValueList))); } } }
void ImportSequenceContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaObjectCollection items, CodeIdentifiers classIds, bool multiValue, ref bool isMixed) { foreach (XmlSchemaObject item in items) { if (item is XmlSchemaElement) { string ns; XmlSchemaElement elem = (XmlSchemaElement) item; XmlTypeMapping emap; TypeData typeData = GetElementTypeData (typeQName, elem, null, out emap); XmlSchemaElement refElem = GetRefElement (typeQName, elem, out ns); if (elem.MaxOccurs == 1 && !multiValue) { XmlTypeMapMemberElement member = null; if (typeData.SchemaType != SchemaTypes.Array) { member = new XmlTypeMapMemberElement (); if (refElem.DefaultValue != null) member.DefaultValue = ImportDefaultValue (typeData, refElem.DefaultValue); } else if (GetTypeMapping (typeData).IsSimpleType) { // It is a simple list (space separated list). // Since this is not supported, map as a single item value member = new XmlTypeMapMemberElement (); #if NET_2_0 // In MS.NET those types are mapped to a string typeData = TypeTranslator.GetTypeData(typeof(string)); #else typeData = typeData.ListItemTypeData; #endif } else member = new XmlTypeMapMemberList (); if (elem.MinOccurs == 0 && typeData.IsValueType) member.IsOptionalValueType = true; member.Name = classIds.AddUnique(CodeIdentifier.MakeValid(refElem.Name), member); member.Documentation = GetDocumentation (elem); member.TypeData = typeData; member.ElementInfo.Add (CreateElementInfo (ns, member, refElem.Name, typeData, refElem.IsNillable, refElem.Form, emap, items.IndexOf (item))); cmap.AddMember (member); } else { XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList (); member.ListMap = new ListMap (); member.Name = classIds.AddUnique(CodeIdentifier.MakeValid(refElem.Name), member); member.Documentation = GetDocumentation (elem); member.TypeData = typeData.ListTypeData; member.ElementInfo.Add (CreateElementInfo (ns, member, refElem.Name, typeData, refElem.IsNillable, refElem.Form, emap, items.IndexOf (item))); member.ListMap.ItemInfo = member.ElementInfo; cmap.AddMember (member); } } else if (item is XmlSchemaAny) { XmlSchemaAny elem = (XmlSchemaAny) item; XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement (); member.Name = classIds.AddUnique ("Any", member); member.Documentation = GetDocumentation (elem); Type ctype; if (elem.MaxOccurs != 1 || multiValue) ctype = isMixed ? typeof(XmlNode[]) : typeof(XmlElement[]); else ctype = isMixed ? typeof(XmlNode) : typeof(XmlElement); member.TypeData = TypeTranslator.GetTypeData (ctype); XmlTypeMapElementInfo einfo = new XmlTypeMapElementInfo (member, member.TypeData); einfo.IsUnnamedAnyElement = true; member.ElementInfo.Add (einfo); if (isMixed) { einfo = CreateTextElementInfo (typeQName.Namespace, member, member.TypeData); member.ElementInfo.Add (einfo); member.IsXmlTextCollector = true; isMixed = false; //Allow only one XmlTextAttribute } cmap.AddMember (member); } else if (item is XmlSchemaParticle) { ImportParticleContent (typeQName, cmap, (XmlSchemaParticle)item, classIds, multiValue, ref isMixed); } } }
ListMap BuildArrayMap (XmlQualifiedName typeQName, XmlSchemaComplexType stype, out TypeData arrayTypeData) { if (encodedFormat) { XmlSchemaComplexContent content = stype.ContentModel as XmlSchemaComplexContent; XmlSchemaComplexContentRestriction rest = content.Content as XmlSchemaComplexContentRestriction; XmlSchemaAttribute arrayTypeAt = FindArrayAttribute (rest.Attributes); if (arrayTypeAt != null) { XmlAttribute[] uatts = arrayTypeAt.UnhandledAttributes; if (uatts == null || uatts.Length == 0) throw new InvalidOperationException ("arrayType attribute not specified in array declaration: " + typeQName); XmlAttribute xat = null; foreach (XmlAttribute at in uatts) if (at.LocalName == "arrayType" && at.NamespaceURI == XmlSerializer.WsdlNamespace) { xat = at; break; } if (xat == null) throw new InvalidOperationException ("arrayType attribute not specified in array declaration: " + typeQName); string name, ns, dims; TypeTranslator.ParseArrayType (xat.Value, out name, out ns, out dims); return BuildEncodedArrayMap (name + dims, ns, out arrayTypeData); } else { XmlSchemaElement elem = null; XmlSchemaSequence seq = rest.Particle as XmlSchemaSequence; if (seq != null && seq.Items.Count == 1) elem = seq.Items[0] as XmlSchemaElement; else { XmlSchemaAll all = rest.Particle as XmlSchemaAll; if (all != null && all.Items.Count == 1) elem = all.Items[0] as XmlSchemaElement; } if (elem == null) throw new InvalidOperationException ("Unknown array format"); return BuildEncodedArrayMap (elem.SchemaTypeName.Name + "[]", elem.SchemaTypeName.Namespace, out arrayTypeData); } } else { ClassMap cmap = new ClassMap (); CodeIdentifiers classIds = new CodeIdentifiers(); ImportParticleComplexContent (typeQName, cmap, stype.Particle, classIds, stype.IsMixed); XmlTypeMapMemberFlatList list = (cmap.AllMembers.Count == 1) ? cmap.AllMembers[0] as XmlTypeMapMemberFlatList : null; if (list != null && list.ChoiceMember == null) { arrayTypeData = list.TypeData; return list.ListMap; } else { arrayTypeData = null; return null; } } }
XmlMemberMapping[] ImportMembersMappingComposite (XmlSchemaComplexType stype, XmlQualifiedName refer) { if (stype.Particle == null) return new XmlMemberMapping [0]; ClassMap cmap = new ClassMap (); XmlSchemaSequence seq = stype.Particle as XmlSchemaSequence; if (seq == null) throw new InvalidOperationException ("Schema element '" + refer + "' cannot be imported as XmlMembersMapping"); CodeIdentifiers classIds = new CodeIdentifiers (); ImportParticleComplexContent (refer, cmap, seq, classIds, false); ImportAttributes (refer, cmap, stype.Attributes, stype.AnyAttribute, classIds); BuildPendingMaps (); int n = 0; XmlMemberMapping[] mapping = new XmlMemberMapping [cmap.AllMembers.Count]; foreach (XmlTypeMapMember mapMem in cmap.AllMembers) mapping[n++] = new XmlMemberMapping (mapMem.Name, refer.Namespace, mapMem, encodedFormat); return mapping; }
void GenerateSetListMembersDefaults (XmlTypeMapping typeMap, ClassMap map, string ob, bool isValueList) { if (map.ListMembers != null) { ArrayList members = map.ListMembers; for (int n=0; n<members.Count; n++) { XmlTypeMapMember mem = (XmlTypeMapMember) members[n]; if (IsReadOnly (typeMap, mem, mem.TypeData, isValueList)) continue; WriteLineInd ("if (" + GenerateGetMemberValue (mem, ob, isValueList) + " == null) {"); GenerateSetMemberValue (mem, ob, GenerateInitializeList (mem.TypeData), isValueList); WriteLineUni ("}"); } } }
private void WriteMembers(ClassMap map, object ob, bool isValueList) { this.WriteAttributeMembers(map, ob, isValueList); this.WriteElementMembers(map, ob, isValueList); }
protected virtual void WriteObjectElementAttributes(XmlTypeMapping typeMap, object ob) { ClassMap map = (ClassMap)typeMap.ObjectMap; this.WriteAttributeMembers(map, ob, false); }
XmlTypeMapping ImportClassMapping(TypeData typeData, string defaultNamespace) { Type type = typeData.Type; if (type.IsValueType) { throw CreateStructException(type); } if (type == typeof(object)) { defaultNamespace = XmlSchema.Namespace; } ReflectionHelper.CheckSerializableType(type, false); XmlTypeMapping map = helper.GetRegisteredClrType(type, GetTypeNamespace(typeData, defaultNamespace)); if (map != null) { return(map); } map = CreateTypeMapping(typeData, null, defaultNamespace); helper.RegisterClrType(map, type, map.Namespace); map.MultiReferenceType = true; ClassMap classMap = new ClassMap(); map.ObjectMap = classMap; // Import members ICollection members = GetReflectionMembers(type); foreach (XmlReflectionMember rmember in members) { if (rmember.SoapAttributes.SoapIgnore) { continue; } classMap.AddMember(CreateMapMember(rmember, defaultNamespace)); } // Import included classes SoapIncludeAttribute[] includes = (SoapIncludeAttribute[])type.GetCustomAttributes(typeof(SoapIncludeAttribute), false); for (int n = 0; n < includes.Length; n++) { Type includedType = includes[n].Type; ImportTypeMapping(includedType); } if (type == typeof(object) && includedTypes != null) { foreach (Type intype in includedTypes) { map.DerivedTypes.Add(ImportTypeMapping(intype)); } } // Register inheritance relations if (type.BaseType != null) { XmlTypeMapping bmap = ImportClassMapping(type.BaseType, defaultNamespace); if (type.BaseType != typeof(object)) { map.BaseMap = bmap; } // At this point, derived classes of this map must be already registered RegisterDerivedMap(bmap, map); } return(map); }
/// <summary>Adds an element declaration to the applicable <see cref="T:System.Xml.Schema.XmlSchema" /> for each of the element parts of a literal SOAP message definition, and specifies whether enclosing elements are included.</summary> /// <param name="xmlMembersMapping">The internal mapping between a .NET Framework type and an XML schema element.</param> /// <param name="exportEnclosingType">true if the schema elements that enclose the schema are to be included; otherwise, false.</param> public void ExportMembersMapping(XmlMembersMapping xmlMembersMapping, bool exportEnclosingType) { ClassMap classMap = (ClassMap)xmlMembersMapping.ObjectMap; if (xmlMembersMapping.HasWrapperElement && exportEnclosingType) { XmlSchema schema = this.GetSchema(xmlMembersMapping.Namespace); XmlSchemaComplexType xmlSchemaComplexType = new XmlSchemaComplexType(); XmlSchemaSequence particle; XmlSchemaAnyAttribute anyAttribute; this.ExportMembersMapSchema(schema, classMap, null, xmlSchemaComplexType.Attributes, out particle, out anyAttribute); xmlSchemaComplexType.Particle = particle; xmlSchemaComplexType.AnyAttribute = anyAttribute; if (this.encodedFormat) { xmlSchemaComplexType.Name = xmlMembersMapping.ElementName; schema.Items.Add(xmlSchemaComplexType); } else { XmlSchemaElement xmlSchemaElement = new XmlSchemaElement(); xmlSchemaElement.Name = xmlMembersMapping.ElementName; xmlSchemaElement.SchemaType = xmlSchemaComplexType; schema.Items.Add(xmlSchemaElement); } } else { ICollection elementMembers = classMap.ElementMembers; if (elementMembers != null) { foreach (object obj in elementMembers) { XmlTypeMapMemberElement xmlTypeMapMemberElement = (XmlTypeMapMemberElement)obj; if (xmlTypeMapMemberElement is XmlTypeMapMemberAnyElement && xmlTypeMapMemberElement.TypeData.IsListType) { XmlSchema schema2 = this.GetSchema(xmlMembersMapping.Namespace); XmlSchemaParticle schemaArrayElement = this.GetSchemaArrayElement(schema2, xmlTypeMapMemberElement.ElementInfo); if (schemaArrayElement is XmlSchemaAny) { XmlSchemaComplexType xmlSchemaComplexType2 = this.FindComplexType(schema2.Items, "any"); if (xmlSchemaComplexType2 != null) { continue; } xmlSchemaComplexType2 = new XmlSchemaComplexType(); xmlSchemaComplexType2.Name = "any"; xmlSchemaComplexType2.IsMixed = true; XmlSchemaSequence xmlSchemaSequence = new XmlSchemaSequence(); xmlSchemaComplexType2.Particle = xmlSchemaSequence; xmlSchemaSequence.Items.Add(schemaArrayElement); schema2.Items.Add(xmlSchemaComplexType2); continue; } } XmlTypeMapElementInfo xmlTypeMapElementInfo = (XmlTypeMapElementInfo)xmlTypeMapMemberElement.ElementInfo[0]; XmlSchema schema3; if (this.encodedFormat) { schema3 = this.GetSchema(xmlMembersMapping.Namespace); this.ImportNamespace(schema3, "http://schemas.xmlsoap.org/soap/encoding/"); } else { schema3 = this.GetSchema(xmlTypeMapElementInfo.Namespace); } XmlSchemaElement xmlSchemaElement2 = this.FindElement(schema3.Items, xmlTypeMapElementInfo.ElementName); XmlSchemaExporter.XmlSchemaObjectContainer container = null; if (!this.encodedFormat) { container = new XmlSchemaExporter.XmlSchemaObjectContainer(schema3); } Type type = xmlTypeMapMemberElement.GetType(); if (xmlTypeMapMemberElement is XmlTypeMapMemberFlatList) { throw new InvalidOperationException("Unwrapped arrays not supported as parameters"); } XmlSchemaElement xmlSchemaElement3; if (type == typeof(XmlTypeMapMemberElement)) { xmlSchemaElement3 = (XmlSchemaElement)this.GetSchemaElement(schema3, xmlTypeMapElementInfo, xmlTypeMapMemberElement.DefaultValue, false, container); } else { xmlSchemaElement3 = (XmlSchemaElement)this.GetSchemaElement(schema3, xmlTypeMapElementInfo, false, container); } if (xmlSchemaElement2 != null) { if (!xmlSchemaElement2.SchemaTypeName.Equals(xmlSchemaElement3.SchemaTypeName)) { string text = "The XML element named '" + xmlTypeMapElementInfo.ElementName + "' "; string text2 = text; text = string.Concat(new string[] { text2, "from namespace '", schema3.TargetNamespace, "' references distinct types ", xmlSchemaElement3.SchemaTypeName.Name, " and ", xmlSchemaElement2.SchemaTypeName.Name, ". " }); text += "Use XML attributes to specify another XML name or namespace for the element or types."; throw new InvalidOperationException(text); } schema3.Items.Remove(xmlSchemaElement3); } } } } this.CompileSchemas(); }
private void ExportMembersMapSchema(XmlSchema schema, ClassMap map, XmlTypeMapping baseMap, XmlSchemaObjectCollection outAttributes, out XmlSchemaSequence particle, out XmlSchemaAnyAttribute anyAttribute) { particle = null; XmlSchemaSequence xmlSchemaSequence = new XmlSchemaSequence(); ICollection elementMembers = map.ElementMembers; if (elementMembers != null && !map.HasSimpleContent) { foreach (object obj in elementMembers) { XmlTypeMapMemberElement xmlTypeMapMemberElement = (XmlTypeMapMemberElement)obj; if (baseMap == null || !this.DefinedInBaseMap(baseMap, xmlTypeMapMemberElement)) { Type type = xmlTypeMapMemberElement.GetType(); if (type == typeof(XmlTypeMapMemberFlatList)) { XmlSchemaParticle schemaArrayElement = this.GetSchemaArrayElement(schema, xmlTypeMapMemberElement.ElementInfo); if (schemaArrayElement != null) { xmlSchemaSequence.Items.Add(schemaArrayElement); } } else if (type == typeof(XmlTypeMapMemberAnyElement)) { xmlSchemaSequence.Items.Add(this.GetSchemaArrayElement(schema, xmlTypeMapMemberElement.ElementInfo)); } else if (type == typeof(XmlTypeMapMemberElement)) { this.GetSchemaElement(schema, (XmlTypeMapElementInfo)xmlTypeMapMemberElement.ElementInfo[0], xmlTypeMapMemberElement.DefaultValue, true, new XmlSchemaExporter.XmlSchemaObjectContainer(xmlSchemaSequence)); } else { this.GetSchemaElement(schema, (XmlTypeMapElementInfo)xmlTypeMapMemberElement.ElementInfo[0], true, new XmlSchemaExporter.XmlSchemaObjectContainer(xmlSchemaSequence)); } } } } if (xmlSchemaSequence.Items.Count > 0) { particle = xmlSchemaSequence; } ICollection attributeMembers = map.AttributeMembers; if (attributeMembers != null) { foreach (object obj2 in attributeMembers) { XmlTypeMapMemberAttribute xmlTypeMapMemberAttribute = (XmlTypeMapMemberAttribute)obj2; if (baseMap == null || !this.DefinedInBaseMap(baseMap, xmlTypeMapMemberAttribute)) { outAttributes.Add(this.GetSchemaAttribute(schema, xmlTypeMapMemberAttribute, true)); } } } XmlTypeMapMember defaultAnyAttributeMember = map.DefaultAnyAttributeMember; if (defaultAnyAttributeMember != null) { anyAttribute = new XmlSchemaAnyAttribute(); } else { anyAttribute = null; } }
private bool BaseHasMixedContent(XmlTypeMapping map) { ClassMap classMap = (ClassMap)map.ObjectMap; return(classMap.XmlTextCollector != null && map.BaseMap != null && this.DefinedInBaseMap(map.BaseMap, classMap.XmlTextCollector)); }
private void ExportClassSchema(XmlTypeMapping map) { if (this.IsMapExported(map)) { return; } this.SetMapExported(map); if (map.TypeData.Type == typeof(object)) { foreach (object obj in map.DerivedTypes) { XmlTypeMapping xmlTypeMapping = (XmlTypeMapping)obj; if (xmlTypeMapping.TypeData.SchemaType == SchemaTypes.Class) { this.ExportClassSchema(xmlTypeMapping); } } return; } XmlSchema schema = this.GetSchema(map.XmlTypeNamespace); XmlSchemaComplexType xmlSchemaComplexType = new XmlSchemaComplexType(); xmlSchemaComplexType.Name = map.XmlType; schema.Items.Add(xmlSchemaComplexType); ClassMap classMap = (ClassMap)map.ObjectMap; if (classMap.HasSimpleContent) { XmlSchemaSimpleContent xmlSchemaSimpleContent = new XmlSchemaSimpleContent(); xmlSchemaComplexType.ContentModel = xmlSchemaSimpleContent; XmlSchemaSimpleContentExtension xmlSchemaSimpleContentExtension = new XmlSchemaSimpleContentExtension(); xmlSchemaSimpleContent.Content = xmlSchemaSimpleContentExtension; XmlSchemaSequence xmlSchemaSequence; XmlSchemaAnyAttribute anyAttribute; this.ExportMembersMapSchema(schema, classMap, map.BaseMap, xmlSchemaSimpleContentExtension.Attributes, out xmlSchemaSequence, out anyAttribute); xmlSchemaSimpleContentExtension.AnyAttribute = anyAttribute; if (map.BaseMap == null) { xmlSchemaSimpleContentExtension.BaseTypeName = classMap.SimpleContentBaseType; } else { xmlSchemaSimpleContentExtension.BaseTypeName = new XmlQualifiedName(map.BaseMap.XmlType, map.BaseMap.XmlTypeNamespace); this.ImportNamespace(schema, map.BaseMap.XmlTypeNamespace); this.ExportClassSchema(map.BaseMap); } } else if (map.BaseMap != null && map.BaseMap.IncludeInSchema) { XmlSchemaComplexContent xmlSchemaComplexContent = new XmlSchemaComplexContent(); XmlSchemaComplexContentExtension xmlSchemaComplexContentExtension = new XmlSchemaComplexContentExtension(); xmlSchemaComplexContentExtension.BaseTypeName = new XmlQualifiedName(map.BaseMap.XmlType, map.BaseMap.XmlTypeNamespace); xmlSchemaComplexContent.Content = xmlSchemaComplexContentExtension; xmlSchemaComplexType.ContentModel = xmlSchemaComplexContent; XmlSchemaSequence particle; XmlSchemaAnyAttribute anyAttribute2; this.ExportMembersMapSchema(schema, classMap, map.BaseMap, xmlSchemaComplexContentExtension.Attributes, out particle, out anyAttribute2); xmlSchemaComplexContentExtension.Particle = particle; xmlSchemaComplexContentExtension.AnyAttribute = anyAttribute2; xmlSchemaComplexType.IsMixed = this.HasMixedContent(map); xmlSchemaComplexContent.IsMixed = this.BaseHasMixedContent(map); this.ImportNamespace(schema, map.BaseMap.XmlTypeNamespace); this.ExportClassSchema(map.BaseMap); } else { XmlSchemaSequence particle2; XmlSchemaAnyAttribute anyAttribute3; this.ExportMembersMapSchema(schema, classMap, map.BaseMap, xmlSchemaComplexType.Attributes, out particle2, out anyAttribute3); xmlSchemaComplexType.Particle = particle2; xmlSchemaComplexType.AnyAttribute = anyAttribute3; xmlSchemaComplexType.IsMixed = (classMap.XmlTextCollector != null); } foreach (object obj2 in map.DerivedTypes) { XmlTypeMapping xmlTypeMapping2 = (XmlTypeMapping)obj2; if (xmlTypeMapping2.TypeData.SchemaType == SchemaTypes.Class) { this.ExportClassSchema(xmlTypeMapping2); } } }
void ExportMembersMapSchema(XmlSchema schema, ClassMap map, XmlTypeMapping baseMap, XmlSchemaObjectCollection outAttributes, out XmlSchemaSequence particle, out XmlSchemaAnyAttribute anyAttribute) { particle = null; XmlSchemaSequence seq = new XmlSchemaSequence(); ICollection members = map.ElementMembers; if (members != null && !map.HasSimpleContent) { foreach (XmlTypeMapMemberElement member in members) { if (baseMap != null && DefinedInBaseMap(baseMap, member)) { continue; } Type memType = member.GetType(); if (memType == typeof(XmlTypeMapMemberFlatList)) { XmlSchemaParticle part = GetSchemaArrayElement(schema, member.ElementInfo); if (part != null) { seq.Items.Add(part); } } else if (memType == typeof(XmlTypeMapMemberAnyElement)) { seq.Items.Add(GetSchemaArrayElement(schema, member.ElementInfo)); } else if (memType == typeof(XmlTypeMapMemberElement)) { GetSchemaElement(schema, (XmlTypeMapElementInfo)member.ElementInfo [0], member.DefaultValue, true, new XmlSchemaObjectContainer(seq)); } else { GetSchemaElement(schema, (XmlTypeMapElementInfo)member.ElementInfo[0], true, new XmlSchemaObjectContainer(seq)); } } } if (seq.Items.Count > 0) { particle = seq; } // Write attributes ICollection attributes = map.AttributeMembers; if (attributes != null) { foreach (XmlTypeMapMemberAttribute attr in attributes) { if (baseMap != null && DefinedInBaseMap(baseMap, attr)) { continue; } outAttributes.Add(GetSchemaAttribute(schema, attr, true)); } } XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember; if (anyAttrMember != null) { anyAttribute = new XmlSchemaAnyAttribute(); } else { anyAttribute = null; } }
void GenerateReadMembers (XmlMapping xmlMap, ClassMap map, string ob, bool isValueList, bool readBySoapOrder) { XmlTypeMapping typeMap = xmlMap as XmlTypeMapping; Type xmlMapType = (typeMap != null) ? typeMap.TypeData.Type : typeof(object[]); bool first = false; // Read attributes GenerateReadAttributeMembers (xmlMap, map, ob, isValueList, ref first); if (!isValueList) { WriteLine ("Reader.MoveToElement();"); WriteLineInd ("if (Reader.IsEmptyElement) {"); WriteLine ("Reader.Skip ();"); GenerateSetListMembersDefaults (typeMap, map, ob, isValueList); WriteLine ("return " + ob + ";"); WriteLineUni ("}"); WriteLine (""); WriteLine ("Reader.ReadStartElement();"); } // Reads elements WriteLine("Reader.MoveToContent();"); WriteLine (""); if (!GenerateReadHook (HookType.elements, xmlMapType)) { string[] readFlag = null; if (map.ElementMembers != null && !readBySoapOrder) { string readFlagsVars = string.Empty; readFlag = new string[map.ElementMembers.Count]; int n=0; foreach (XmlTypeMapMember mem in map.ElementMembers) { // The text collector doesn't need a flag if (!((mem is XmlTypeMapMemberElement) && ((XmlTypeMapMemberElement)mem).IsXmlTextCollector)) { readFlag[n] = GetBoolTempVar (); if (readFlagsVars.Length > 0) readFlagsVars += ", "; readFlagsVars += readFlag[n] + "=false"; } n++; } if (readFlagsVars.Length > 0) { readFlagsVars = "bool " + readFlagsVars; WriteLine (readFlagsVars + ";"); } foreach (XmlTypeMapElementInfo info in map.AllElementInfos) if (info.ExplicitOrder >= 0) { WriteLine ("int idx = -1;"); break; } WriteLine (""); } string[] indexes = null; string[] flatLists = null; string[] flatListsChoices = null; if (map.FlatLists != null) { indexes = new string[map.FlatLists.Count]; flatLists = new string[map.FlatLists.Count]; string code = "int "; for (int n=0; n<map.FlatLists.Count; n++) { XmlTypeMapMemberElement mem = (XmlTypeMapMemberElement)map.FlatLists[n]; indexes[n] = GetNumTempVar (); if (n > 0) code += ", "; code += indexes[n] + "=0"; if (!MemberHasReadReplaceHook (xmlMapType, mem)) { flatLists[n] = GetObTempVar (); string rval; WriteLine (mem.TypeData.CSharpFullName + " " + flatLists[n] + ";"); if (IsReadOnly (typeMap, mem, mem.TypeData, isValueList)) { rval = GenerateGetMemberValue (mem, ob, isValueList); WriteLine (flatLists[n] + " = " + rval + ";"); } else if (mem.TypeData.Type.IsArray) { rval = GenerateInitializeList (mem.TypeData); WriteLine (flatLists[n] + " = " + rval + ";"); } else { WriteLine (flatLists[n] + " = " + GenerateGetMemberValue (mem, ob, isValueList) + ";"); WriteLineInd ("if (((object)" + flatLists[n] + ") == null) {"); WriteLine (flatLists[n] + " = " + GenerateInitializeList (mem.TypeData) + ";"); GenerateSetMemberValue (mem, ob, flatLists[n], isValueList); WriteLineUni ("}"); } } if (mem.ChoiceMember != null) { if (flatListsChoices == null) flatListsChoices = new string [map.FlatLists.Count]; flatListsChoices[n] = GetObTempVar (); string rval = GenerateInitializeList (mem.ChoiceTypeData); WriteLine (mem.ChoiceTypeData.CSharpFullName + " " + flatListsChoices[n] + " = " + rval + ";"); } } WriteLine (code + ";"); WriteLine (""); } if (_format == SerializationFormat.Encoded && map.ElementMembers != null) { _fixupCallbacks.Add (xmlMap); WriteLine ("Fixup fixup = new Fixup(" + ob + ", new XmlSerializationFixupCallback(" + GetFixupCallbackName (xmlMap) + "), " + map.ElementMembers.Count + ");"); WriteLine ("AddFixup (fixup);"); WriteLine (""); } ArrayList infos = null; int maxInd; if (readBySoapOrder) { if (map.ElementMembers != null) maxInd = map.ElementMembers.Count; else maxInd = 0; } else { infos = new ArrayList (); infos.AddRange (map.AllElementInfos); maxInd = infos.Count; WriteLine ("while (Reader.NodeType != System.Xml.XmlNodeType.EndElement) "); WriteLineInd ("{"); WriteLine ("if (Reader.NodeType == System.Xml.XmlNodeType.Element) "); WriteLineInd ("{"); } first = true; for (int ind = 0; ind < maxInd; ind++) { XmlTypeMapElementInfo info = readBySoapOrder ? map.GetElement (ind) : (XmlTypeMapElementInfo) infos [ind]; if (!readBySoapOrder) { if (info.IsTextElement || info.IsUnnamedAnyElement) continue; string elemCond = first ? "" : "else "; elemCond += "if ("; if (info.ExplicitOrder >= 0) elemCond += "idx < " + info.ExplicitOrder + "&& "; if (!(info.Member.IsReturnValue && _format == SerializationFormat.Encoded)) { elemCond += "Reader.LocalName == " + GetLiteral (info.ElementName); if (!map.IgnoreMemberNamespace) elemCond += " && Reader.NamespaceURI == " + GetLiteral (info.Namespace); elemCond += " && "; } if (readFlag[info.Member.Index] != null) elemCond += "!" + readFlag[info.Member.Index] + ") {"; else elemCond += "true) {"; WriteLineInd (elemCond); } if (info.Member.GetType() == typeof (XmlTypeMapMemberList)) { if (_format == SerializationFormat.Encoded && info.MultiReferenceType) { string list = GetObTempVar (); WriteLine ("object " + list + " = ReadReferencingElement (out fixup.Ids[" + info.Member.Index + "]);"); RegisterReferencingMap (info.MappedType); WriteLineInd ("if (fixup.Ids[" + info.Member.Index + "] == null) {"); // Already read if (IsReadOnly (typeMap, info.Member, info.TypeData, isValueList)) WriteLine ("throw CreateReadOnlyCollectionException (" + GetLiteral(info.TypeData.CSharpFullName) + ");"); else GenerateSetMemberValue (info.Member, ob, GetCast (info.Member.TypeData,list), isValueList); WriteLineUni ("}"); if (!info.MappedType.TypeData.Type.IsArray) { WriteLineInd ("else {"); if (IsReadOnly (typeMap, info.Member, info.TypeData, isValueList)) WriteLine (list + " = " + GenerateGetMemberValue (info.Member, ob, isValueList) + ";"); else { WriteLine (list + " = " + GenerateCreateList (info.MappedType.TypeData.Type) + ";"); GenerateSetMemberValue (info.Member, ob, GetCast (info.Member.TypeData,list), isValueList); } WriteLine ("AddFixup (new CollectionFixup (" + list + ", new XmlSerializationCollectionFixupCallback (" + GetFillListName(info.Member.TypeData) + "), fixup.Ids[" + info.Member.Index + "]));"); WriteLine ("fixup.Ids[" + info.Member.Index + "] = null;"); // The member already has the value, no further fix needed. WriteLineUni ("}"); } } else { if (!GenerateReadMemberHook (xmlMapType, info.Member)) { if (IsReadOnly (typeMap, info.Member, info.TypeData, isValueList)) { GenerateReadListElement (info.MappedType, GenerateGetMemberValue (info.Member, ob, isValueList), GetLiteral(info.IsNullable), false); } else if (info.MappedType.TypeData.Type.IsArray) { if (info.IsNullable) GenerateSetMemberValue (info.Member, ob, GenerateReadListElement (info.MappedType, null, GetLiteral(info.IsNullable), true), isValueList); else { string list = GetObTempVar (); WriteLine (info.MappedType.TypeData.CSharpFullName + " " + list + " = " + GenerateReadListElement (info.MappedType, null, GetLiteral(info.IsNullable), true) + ";"); WriteLineInd ("if (((object)" + list + ") != null) {"); GenerateSetMemberValue (info.Member, ob, list, isValueList); WriteLineUni ("}"); } } else { string list = GetObTempVar (); WriteLine (info.MappedType.TypeData.CSharpFullName + " " + list + " = " + GenerateGetMemberValue (info.Member, ob, isValueList) + ";"); WriteLineInd ("if (((object)" + list + ") == null) {"); WriteLine (list + " = " + GenerateCreateList (info.MappedType.TypeData.Type) + ";"); GenerateSetMemberValue (info.Member, ob, list, isValueList); WriteLineUni ("}"); GenerateReadListElement (info.MappedType, list, GetLiteral(info.IsNullable), true); } GenerateEndHook (); } } if (!readBySoapOrder) WriteLine (readFlag[info.Member.Index] + " = true;"); } else if (info.Member.GetType() == typeof (XmlTypeMapMemberFlatList)) { XmlTypeMapMemberFlatList mem = (XmlTypeMapMemberFlatList)info.Member; if (!GenerateReadArrayMemberHook (xmlMapType, info.Member, indexes[mem.FlatArrayIndex])) { GenerateAddListValue (mem.TypeData, flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex], GenerateReadObjectElement (info), !IsReadOnly (typeMap, info.Member, info.TypeData, isValueList)); if (mem.ChoiceMember != null) { GenerateAddListValue (mem.ChoiceTypeData, flatListsChoices[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex], GetLiteral (info.ChoiceValue), true); } GenerateEndHook (); } WriteLine (indexes[mem.FlatArrayIndex] + "++;"); } else if (info.Member.GetType() == typeof (XmlTypeMapMemberAnyElement)) { XmlTypeMapMemberAnyElement mem = (XmlTypeMapMemberAnyElement)info.Member; if (mem.TypeData.IsListType) { if (!GenerateReadArrayMemberHook (xmlMapType, info.Member, indexes[mem.FlatArrayIndex])) { GenerateAddListValue (mem.TypeData, flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex], GetReadXmlNode (mem.TypeData.ListItemTypeData, false), true); GenerateEndHook (); } WriteLine (indexes[mem.FlatArrayIndex] + "++;"); } else { if (!GenerateReadMemberHook (xmlMapType, info.Member)) { GenerateSetMemberValue (mem, ob, GetReadXmlNode(mem.TypeData, false), isValueList); GenerateEndHook (); } } } else if (info.Member.GetType() == typeof(XmlTypeMapMemberElement)) { if (!readBySoapOrder) WriteLine (readFlag[info.Member.Index] + " = true;"); if (info.ExplicitOrder >= 0) WriteLine ("idx = " + info.ExplicitOrder + ";"); if (_format == SerializationFormat.Encoded) { string val = GetObTempVar (); RegisterReferencingMap (info.MappedType); if (info.Member.TypeData.SchemaType != SchemaTypes.Primitive) WriteLine ("object " + val + " = ReadReferencingElement (out fixup.Ids[" + info.Member.Index + "]);"); else WriteLine ("object " + val + " = ReadReferencingElement (" + GetLiteral(info.Member.TypeData.XmlType) + ", " + GetLiteral(System.Xml.Schema.XmlSchema.Namespace) + ", out fixup.Ids[" + info.Member.Index + "]);"); if (info.MultiReferenceType) WriteLineInd ("if (fixup.Ids[" + info.Member.Index + "] == null) {"); // already read else WriteLineInd ("if (" + val + " != null) {"); // null value GenerateSetMemberValue (info.Member, ob, GetCast (info.Member.TypeData,val), isValueList); WriteLineUni ("}"); } else if (!GenerateReadMemberHook (xmlMapType, info.Member)) { if (info.ChoiceValue != null) { XmlTypeMapMemberElement imem = (XmlTypeMapMemberElement) info.Member; WriteLine (ob + ".@" + imem.ChoiceMember + " = " + GetLiteral(info.ChoiceValue) + ";"); } GenerateSetMemberValue (info.Member, ob, GenerateReadObjectElement (info), isValueList); GenerateEndHook (); } } else throw new InvalidOperationException ("Unknown member type"); if (!readBySoapOrder) WriteLineUni ("}"); else WriteLine ("Reader.MoveToContent();"); first = false; } if (!readBySoapOrder) { if (!first) WriteLineInd ("else {"); if (map.DefaultAnyElementMember != null) { XmlTypeMapMemberAnyElement mem = map.DefaultAnyElementMember; if (mem.TypeData.IsListType) { if (!GenerateReadArrayMemberHook (xmlMapType, mem, indexes[mem.FlatArrayIndex])) { GenerateAddListValue (mem.TypeData, flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex], GetReadXmlNode(mem.TypeData.ListItemTypeData, false), true); GenerateEndHook (); } WriteLine (indexes[mem.FlatArrayIndex] + "++;"); } else if (! GenerateReadMemberHook (xmlMapType, mem)) { GenerateSetMemberValue (mem, ob, GetReadXmlNode(mem.TypeData, false), isValueList); GenerateEndHook (); } } else { if (!GenerateReadHook (HookType.unknownElement, xmlMapType)) { WriteLine ("UnknownNode (" + ob + ");"); GenerateEndHook (); } } if (!first) WriteLineUni ("}"); WriteLineUni ("}"); if (map.XmlTextCollector != null) { WriteLine ("else if (Reader.NodeType == System.Xml.XmlNodeType.Text || Reader.NodeType == System.Xml.XmlNodeType.CDATA)"); WriteLineInd ("{"); if (map.XmlTextCollector is XmlTypeMapMemberExpandable) { XmlTypeMapMemberExpandable mem = (XmlTypeMapMemberExpandable)map.XmlTextCollector; XmlTypeMapMemberFlatList flatl = mem as XmlTypeMapMemberFlatList; TypeData itype = (flatl == null) ? mem.TypeData.ListItemTypeData : flatl.ListMap.FindTextElement().TypeData; if (!GenerateReadArrayMemberHook (xmlMapType, map.XmlTextCollector, indexes[mem.FlatArrayIndex])) { string val = (itype.Type == typeof (string)) ? "Reader.ReadString()" : GetReadXmlNode (itype, false); GenerateAddListValue (mem.TypeData, flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex], val, true); GenerateEndHook (); } WriteLine (indexes[mem.FlatArrayIndex] + "++;"); } else if (!GenerateReadMemberHook (xmlMapType, map.XmlTextCollector)) { XmlTypeMapMemberElement mem = (XmlTypeMapMemberElement) map.XmlTextCollector; XmlTypeMapElementInfo info = (XmlTypeMapElementInfo) mem.ElementInfo [0]; if (info.TypeData.Type == typeof (string)) GenerateSetMemberValue (mem, ob, "ReadString (" + GenerateGetMemberValue (mem, ob, isValueList) + ")", isValueList); else { WriteLineInd ("{"); string str = GetStrTempVar (); WriteLine ("string " + str + " = Reader.ReadString();"); GenerateSetMemberValue (mem, ob, GenerateGetValueFromXmlString (str, info.TypeData, info.MappedType, info.IsNullable), isValueList); WriteLineUni ("}"); } GenerateEndHook (); } WriteLineUni ("}"); } WriteLine ("else"); WriteLine ("\tUnknownNode(" + ob + ");"); WriteLine (""); WriteLine ("Reader.MoveToContent();"); WriteLineUni ("}"); } else WriteLine ("Reader.MoveToContent();"); if (flatLists != null) { WriteLine (""); foreach (XmlTypeMapMemberExpandable mem in map.FlatLists) { if (MemberHasReadReplaceHook (xmlMapType, mem)) continue; string list = flatLists[mem.FlatArrayIndex]; if (mem.TypeData.Type.IsArray) WriteLine (list + " = (" + mem.TypeData.CSharpFullName + ") ShrinkArray (" + list + ", " + indexes[mem.FlatArrayIndex] + ", " + GetTypeOf(mem.TypeData.Type.GetElementType()) + ", true);"); if (!IsReadOnly (typeMap, mem, mem.TypeData, isValueList) && mem.TypeData.Type.IsArray) GenerateSetMemberValue (mem, ob, list, isValueList); } } if (flatListsChoices != null) { WriteLine (""); foreach (XmlTypeMapMemberExpandable mem in map.FlatLists) { if (MemberHasReadReplaceHook (xmlMapType, mem)) continue; if (mem.ChoiceMember == null) continue; string list = flatListsChoices[mem.FlatArrayIndex]; WriteLine (list + " = (" + mem.ChoiceTypeData.CSharpFullName + ") ShrinkArray (" + list + ", " + indexes[mem.FlatArrayIndex] + ", " + GetTypeOf(mem.ChoiceTypeData.Type.GetElementType()) + ", true);"); WriteLine (ob + ".@" + mem.ChoiceMember + " = " + list + ";"); } } GenerateSetListMembersDefaults (typeMap, map, ob, isValueList); GenerateEndHook (); } if (!isValueList) { WriteLine (""); WriteLine ("ReadEndElement();"); } }
private void ReadMembers(ClassMap map, object ob, bool isValueList, bool readByOrder) { this.ReadAttributeMembers(map, ob, isValueList); if (!isValueList) { base.Reader.MoveToElement(); if (base.Reader.IsEmptyElement) { this.SetListMembersDefaults(map, ob, isValueList); return; } base.Reader.ReadStartElement(); } bool[] array = new bool[(map.ElementMembers == null) ? 0 : map.ElementMembers.Count]; bool flag = isValueList && this._format == SerializationFormat.Encoded && map.ReturnMember != null; base.Reader.MoveToContent(); int[] array2 = null; object[] array3 = null; object[] array4 = null; XmlSerializationReader.Fixup fixup = null; int num = 0; int num2; if (readByOrder) { if (map.ElementMembers != null) { num2 = map.ElementMembers.Count; } else { num2 = 0; } } else { num2 = int.MaxValue; } if (map.FlatLists != null) { array2 = new int[map.FlatLists.Count]; array3 = new object[map.FlatLists.Count]; foreach (object obj in map.FlatLists) { XmlTypeMapMemberExpandable xmlTypeMapMemberExpandable = (XmlTypeMapMemberExpandable)obj; if (this.IsReadOnly(xmlTypeMapMemberExpandable, xmlTypeMapMemberExpandable.TypeData, ob, isValueList)) { array3[xmlTypeMapMemberExpandable.FlatArrayIndex] = xmlTypeMapMemberExpandable.GetValue(ob); } else if (xmlTypeMapMemberExpandable.TypeData.Type.IsArray) { array3[xmlTypeMapMemberExpandable.FlatArrayIndex] = this.InitializeList(xmlTypeMapMemberExpandable.TypeData); } else { object obj2 = xmlTypeMapMemberExpandable.GetValue(ob); if (obj2 == null) { obj2 = this.InitializeList(xmlTypeMapMemberExpandable.TypeData); this.SetMemberValue(xmlTypeMapMemberExpandable, ob, obj2, isValueList); } array3[xmlTypeMapMemberExpandable.FlatArrayIndex] = obj2; } if (xmlTypeMapMemberExpandable.ChoiceMember != null) { if (array4 == null) { array4 = new object[map.FlatLists.Count]; } array4[xmlTypeMapMemberExpandable.FlatArrayIndex] = this.InitializeList(xmlTypeMapMemberExpandable.ChoiceTypeData); } } } if (this._format == SerializationFormat.Encoded && map.ElementMembers != null) { XmlSerializationReaderInterpreter.FixupCallbackInfo @object = new XmlSerializationReaderInterpreter.FixupCallbackInfo(this, map, isValueList); fixup = new XmlSerializationReader.Fixup(ob, new XmlSerializationFixupCallback(@object.FixupMembers), map.ElementMembers.Count); base.AddFixup(fixup); } while (base.Reader.NodeType != XmlNodeType.EndElement && num < num2) { if (base.Reader.NodeType == XmlNodeType.Element) { XmlTypeMapElementInfo xmlTypeMapElementInfo; if (readByOrder) { xmlTypeMapElementInfo = map.GetElement(num++); } else if (flag) { xmlTypeMapElementInfo = (XmlTypeMapElementInfo)((XmlTypeMapMemberElement)map.ReturnMember).ElementInfo[0]; flag = false; } else { xmlTypeMapElementInfo = map.GetElement(base.Reader.LocalName, base.Reader.NamespaceURI); } if (xmlTypeMapElementInfo != null && !array[xmlTypeMapElementInfo.Member.Index]) { if (xmlTypeMapElementInfo.Member.GetType() == typeof(XmlTypeMapMemberList)) { if (this._format == SerializationFormat.Encoded && xmlTypeMapElementInfo.MultiReferenceType) { object obj3 = base.ReadReferencingElement(out fixup.Ids[xmlTypeMapElementInfo.Member.Index]); if (fixup.Ids[xmlTypeMapElementInfo.Member.Index] == null) { if (this.IsReadOnly(xmlTypeMapElementInfo.Member, xmlTypeMapElementInfo.TypeData, ob, isValueList)) { throw base.CreateReadOnlyCollectionException(xmlTypeMapElementInfo.TypeData.FullTypeName); } this.SetMemberValue(xmlTypeMapElementInfo.Member, ob, obj3, isValueList); } else if (!xmlTypeMapElementInfo.MappedType.TypeData.Type.IsArray) { if (this.IsReadOnly(xmlTypeMapElementInfo.Member, xmlTypeMapElementInfo.TypeData, ob, isValueList)) { obj3 = this.GetMemberValue(xmlTypeMapElementInfo.Member, ob, isValueList); } else { obj3 = this.CreateList(xmlTypeMapElementInfo.MappedType.TypeData.Type); this.SetMemberValue(xmlTypeMapElementInfo.Member, ob, obj3, isValueList); } base.AddFixup(new XmlSerializationReader.CollectionFixup(obj3, new XmlSerializationCollectionFixupCallback(this.FillList), fixup.Ids[xmlTypeMapElementInfo.Member.Index])); fixup.Ids[xmlTypeMapElementInfo.Member.Index] = null; } } else if (this.IsReadOnly(xmlTypeMapElementInfo.Member, xmlTypeMapElementInfo.TypeData, ob, isValueList)) { this.ReadListElement(xmlTypeMapElementInfo.MappedType, xmlTypeMapElementInfo.IsNullable, this.GetMemberValue(xmlTypeMapElementInfo.Member, ob, isValueList), false); } else if (xmlTypeMapElementInfo.MappedType.TypeData.Type.IsArray) { object obj4 = this.ReadListElement(xmlTypeMapElementInfo.MappedType, xmlTypeMapElementInfo.IsNullable, null, true); if (obj4 != null || xmlTypeMapElementInfo.IsNullable) { this.SetMemberValue(xmlTypeMapElementInfo.Member, ob, obj4, isValueList); } } else { object obj5 = this.GetMemberValue(xmlTypeMapElementInfo.Member, ob, isValueList); if (obj5 == null) { obj5 = this.CreateList(xmlTypeMapElementInfo.MappedType.TypeData.Type); this.SetMemberValue(xmlTypeMapElementInfo.Member, ob, obj5, isValueList); } this.ReadListElement(xmlTypeMapElementInfo.MappedType, xmlTypeMapElementInfo.IsNullable, obj5, true); } array[xmlTypeMapElementInfo.Member.Index] = true; } else if (xmlTypeMapElementInfo.Member.GetType() == typeof(XmlTypeMapMemberFlatList)) { XmlTypeMapMemberFlatList xmlTypeMapMemberFlatList = (XmlTypeMapMemberFlatList)xmlTypeMapElementInfo.Member; this.AddListValue(xmlTypeMapMemberFlatList.TypeData, ref array3[xmlTypeMapMemberFlatList.FlatArrayIndex], array2[xmlTypeMapMemberFlatList.FlatArrayIndex]++, this.ReadObjectElement(xmlTypeMapElementInfo), !this.IsReadOnly(xmlTypeMapElementInfo.Member, xmlTypeMapElementInfo.TypeData, ob, isValueList)); if (xmlTypeMapMemberFlatList.ChoiceMember != null) { this.AddListValue(xmlTypeMapMemberFlatList.ChoiceTypeData, ref array4[xmlTypeMapMemberFlatList.FlatArrayIndex], array2[xmlTypeMapMemberFlatList.FlatArrayIndex] - 1, xmlTypeMapElementInfo.ChoiceValue, true); } } else if (xmlTypeMapElementInfo.Member.GetType() == typeof(XmlTypeMapMemberAnyElement)) { XmlTypeMapMemberAnyElement xmlTypeMapMemberAnyElement = (XmlTypeMapMemberAnyElement)xmlTypeMapElementInfo.Member; if (xmlTypeMapMemberAnyElement.TypeData.IsListType) { this.AddListValue(xmlTypeMapMemberAnyElement.TypeData, ref array3[xmlTypeMapMemberAnyElement.FlatArrayIndex], array2[xmlTypeMapMemberAnyElement.FlatArrayIndex]++, this.ReadXmlNode(xmlTypeMapMemberAnyElement.TypeData.ListItemTypeData, false), true); } else { this.SetMemberValue(xmlTypeMapMemberAnyElement, ob, this.ReadXmlNode(xmlTypeMapMemberAnyElement.TypeData, false), isValueList); } } else { if (xmlTypeMapElementInfo.Member.GetType() != typeof(XmlTypeMapMemberElement)) { throw new InvalidOperationException("Unknown member type"); } array[xmlTypeMapElementInfo.Member.Index] = true; if (this._format == SerializationFormat.Encoded) { object obj6; if (xmlTypeMapElementInfo.Member.TypeData.SchemaType != SchemaTypes.Primitive) { obj6 = base.ReadReferencingElement(out fixup.Ids[xmlTypeMapElementInfo.Member.Index]); } else { obj6 = base.ReadReferencingElement(xmlTypeMapElementInfo.Member.TypeData.XmlType, "http://www.w3.org/2001/XMLSchema", out fixup.Ids[xmlTypeMapElementInfo.Member.Index]); } if (xmlTypeMapElementInfo.MultiReferenceType) { if (fixup.Ids[xmlTypeMapElementInfo.Member.Index] == null) { this.SetMemberValue(xmlTypeMapElementInfo.Member, ob, obj6, isValueList); } } else if (obj6 != null) { this.SetMemberValue(xmlTypeMapElementInfo.Member, ob, obj6, isValueList); } } else { this.SetMemberValue(xmlTypeMapElementInfo.Member, ob, this.ReadObjectElement(xmlTypeMapElementInfo), isValueList); if (xmlTypeMapElementInfo.ChoiceValue != null) { XmlTypeMapMemberElement xmlTypeMapMemberElement = (XmlTypeMapMemberElement)xmlTypeMapElementInfo.Member; xmlTypeMapMemberElement.SetChoice(ob, xmlTypeMapElementInfo.ChoiceValue); } } } } else if (map.DefaultAnyElementMember != null) { XmlTypeMapMemberAnyElement defaultAnyElementMember = map.DefaultAnyElementMember; if (defaultAnyElementMember.TypeData.IsListType) { this.AddListValue(defaultAnyElementMember.TypeData, ref array3[defaultAnyElementMember.FlatArrayIndex], array2[defaultAnyElementMember.FlatArrayIndex]++, this.ReadXmlNode(defaultAnyElementMember.TypeData.ListItemTypeData, false), true); } else { this.SetMemberValue(defaultAnyElementMember, ob, this.ReadXmlNode(defaultAnyElementMember.TypeData, false), isValueList); } } else { this.ProcessUnknownElement(ob); } } else if ((base.Reader.NodeType == XmlNodeType.Text || base.Reader.NodeType == XmlNodeType.CDATA) && map.XmlTextCollector != null) { if (map.XmlTextCollector is XmlTypeMapMemberExpandable) { XmlTypeMapMemberExpandable xmlTypeMapMemberExpandable2 = (XmlTypeMapMemberExpandable)map.XmlTextCollector; XmlTypeMapMemberFlatList xmlTypeMapMemberFlatList2 = xmlTypeMapMemberExpandable2 as XmlTypeMapMemberFlatList; TypeData typeData = (xmlTypeMapMemberFlatList2 != null) ? xmlTypeMapMemberFlatList2.ListMap.FindTextElement().TypeData : xmlTypeMapMemberExpandable2.TypeData.ListItemTypeData; object value = (typeData.Type != typeof(string)) ? this.ReadXmlNode(typeData, false) : base.Reader.ReadString(); this.AddListValue(xmlTypeMapMemberExpandable2.TypeData, ref array3[xmlTypeMapMemberExpandable2.FlatArrayIndex], array2[xmlTypeMapMemberExpandable2.FlatArrayIndex]++, value, true); } else { XmlTypeMapMemberElement xmlTypeMapMemberElement2 = (XmlTypeMapMemberElement)map.XmlTextCollector; XmlTypeMapElementInfo xmlTypeMapElementInfo2 = (XmlTypeMapElementInfo)xmlTypeMapMemberElement2.ElementInfo[0]; if (xmlTypeMapElementInfo2.TypeData.Type == typeof(string)) { this.SetMemberValue(xmlTypeMapMemberElement2, ob, base.ReadString((string)this.GetMemberValue(xmlTypeMapMemberElement2, ob, isValueList)), isValueList); } else { this.SetMemberValue(xmlTypeMapMemberElement2, ob, this.GetValueFromXmlString(base.Reader.ReadString(), xmlTypeMapElementInfo2.TypeData, xmlTypeMapElementInfo2.MappedType), isValueList); } } } else { base.UnknownNode(ob); } base.Reader.MoveToContent(); } if (array3 != null) { foreach (object obj7 in map.FlatLists) { XmlTypeMapMemberExpandable xmlTypeMapMemberExpandable3 = (XmlTypeMapMemberExpandable)obj7; object obj8 = array3[xmlTypeMapMemberExpandable3.FlatArrayIndex]; if (xmlTypeMapMemberExpandable3.TypeData.Type.IsArray) { obj8 = base.ShrinkArray((Array)obj8, array2[xmlTypeMapMemberExpandable3.FlatArrayIndex], xmlTypeMapMemberExpandable3.TypeData.Type.GetElementType(), true); } if (!this.IsReadOnly(xmlTypeMapMemberExpandable3, xmlTypeMapMemberExpandable3.TypeData, ob, isValueList) && xmlTypeMapMemberExpandable3.TypeData.Type.IsArray) { this.SetMemberValue(xmlTypeMapMemberExpandable3, ob, obj8, isValueList); } } } if (array4 != null) { foreach (object obj9 in map.FlatLists) { XmlTypeMapMemberExpandable xmlTypeMapMemberExpandable4 = (XmlTypeMapMemberExpandable)obj9; object obj10 = array4[xmlTypeMapMemberExpandable4.FlatArrayIndex]; if (obj10 != null) { obj10 = base.ShrinkArray((Array)obj10, array2[xmlTypeMapMemberExpandable4.FlatArrayIndex], xmlTypeMapMemberExpandable4.ChoiceTypeData.Type.GetElementType(), true); XmlTypeMapMember.SetValue(ob, xmlTypeMapMemberExpandable4.ChoiceMember, obj10); } } } this.SetListMembersDefaults(map, ob, isValueList); }
public XmlMembersMapping ImportAnyType (XmlQualifiedName typeName, string elementName) { if (typeName == XmlQualifiedName.Empty) { XmlTypeMapMemberAnyElement mapMem = new XmlTypeMapMemberAnyElement (); mapMem.Name = typeName.Name; mapMem.TypeData = TypeTranslator.GetTypeData(typeof(XmlNode)); mapMem.ElementInfo.Add (CreateElementInfo (typeName.Namespace, mapMem, typeName.Name, mapMem.TypeData, true, XmlSchemaForm.None, -1)); XmlMemberMapping[] mm = new XmlMemberMapping [1]; mm[0] = new XmlMemberMapping (typeName.Name, typeName.Namespace, mapMem, encodedFormat); return new XmlMembersMapping (mm); } else { XmlSchemaComplexType stype = (XmlSchemaComplexType) schemas.Find (typeName, typeof (XmlSchemaComplexType)); if (stype == null) throw new InvalidOperationException ("Referenced type '" + typeName + "' not found"); if (!CanBeAnyElement (stype)) throw new InvalidOperationException ("The type '" + typeName + "' is not valid for a collection of any elements"); ClassMap cmap = new ClassMap (); CodeIdentifiers classIds = new CodeIdentifiers (); bool isMixed = stype.IsMixed; ImportSequenceContent (typeName, cmap, ((XmlSchemaSequence) stype.Particle).Items, classIds, false, ref isMixed); XmlTypeMapMemberAnyElement mapMem = (XmlTypeMapMemberAnyElement) cmap.AllMembers[0]; mapMem.Name = typeName.Name; XmlMemberMapping[] mm = new XmlMemberMapping [1]; mm[0] = new XmlMemberMapping (typeName.Name, typeName.Namespace, mapMem, encodedFormat); return new XmlMembersMapping (mm); } }
// Attributes might be redefined, so there is an existing attribute for the same name, skip it. // FIXME: this is nothing more than just a hack. // Basically it should use // XmlSchemaComplexType.AttributeUses. XmlSchemaObjectCollection CollectAttributeUsesNonOverlap ( XmlSchemaObjectCollection src, ClassMap map) { XmlSchemaObjectCollection atts = new XmlSchemaObjectCollection (); foreach (var a in EnumerateAttributes (src, new List<XmlSchemaAttributeGroup> ())) if (map.GetAttribute (a.QualifiedName.Name, a.QualifiedName.Namespace) == null) atts.Add (a); return atts; }
void ImportAttributes (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaObjectCollection atts, XmlSchemaAnyAttribute anyat, CodeIdentifiers classIds) { atts = CollectAttributeUsesNonOverlap (atts, cmap); if (anyat != null) { XmlTypeMapMemberAnyAttribute member = new XmlTypeMapMemberAnyAttribute (); member.Name = classIds.AddUnique ("AnyAttribute", member); member.TypeData = TypeTranslator.GetTypeData (typeof(XmlAttribute[])); cmap.AddMember (member); } foreach (XmlSchemaObject at in atts) { if (at is XmlSchemaAttribute) { string ns; XmlSchemaAttribute attr = (XmlSchemaAttribute)at; XmlSchemaAttribute refAttr = GetRefAttribute (typeQName, attr, out ns); XmlTypeMapMemberAttribute member = new XmlTypeMapMemberAttribute (); member.Name = classIds.AddUnique (CodeIdentifier.MakeValid (refAttr.Name), member); member.Documentation = GetDocumentation (attr); member.AttributeName = refAttr.Name; member.Namespace = ns; member.Form = refAttr.Form; member.TypeData = GetAttributeTypeData (typeQName, attr); if (refAttr.DefaultValue != null) member.DefaultValue = ImportDefaultValue (member.TypeData, refAttr.DefaultValue); else if (member.TypeData.IsValueType) member.IsOptionalValueType = (refAttr.ValidatedUse != XmlSchemaUse.Required); if (member.TypeData.IsComplexType) member.MappedType = GetTypeMapping (member.TypeData); cmap.AddMember (member); } else if (at is XmlSchemaAttributeGroupRef) { XmlSchemaAttributeGroupRef gref = (XmlSchemaAttributeGroupRef)at; XmlSchemaAttributeGroup grp = FindRefAttributeGroup (gref.RefName); ImportAttributes (typeQName, cmap, grp.Attributes, grp.AnyAttribute, classIds); } } }
void ImportParticleComplexContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaParticle particle, CodeIdentifiers classIds, bool isMixed) { ImportParticleContent (typeQName, cmap, particle, classIds, false, ref isMixed); if (isMixed && cmap.XmlTextCollector == null) { XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList (); member.Name = classIds.AddUnique ("Text", member); member.TypeData = TypeTranslator.GetTypeData (typeof(string[])); member.ElementInfo.Add (CreateTextElementInfo (typeQName.Namespace, member, member.TypeData.ListItemTypeData)); member.IsXmlTextCollector = true; member.ListMap = new ListMap (); member.ListMap.ItemInfo = member.ElementInfo; cmap.AddMember (member); } }
void AddTextMember (XmlQualifiedName typeQName, ClassMap cmap, CodeIdentifiers classIds) { if (cmap.XmlTextCollector == null) { XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList (); member.Name = classIds.AddUnique ("Text", member); member.TypeData = TypeTranslator.GetTypeData (typeof(string[])); member.ElementInfo.Add (CreateTextElementInfo (typeQName.Namespace, member, member.TypeData.ListItemTypeData)); member.IsXmlTextCollector = true; member.ListMap = new ListMap (); member.ListMap.ItemInfo = member.ElementInfo; cmap.AddMember (member); } }
public FixupCallbackInfo(XmlSerializationReaderInterpreter sri, ClassMap map, bool isValueList) { _sri = sri; _map = map; _isValueList = isValueList; }
void ReadMembers(ClassMap map, object ob, bool isValueList, bool readByOrder) { // Set the default values of the members if (map.MembersWithDefault != null) { ArrayList members = map.MembersWithDefault; for (int n = 0; n < members.Count; n++) { XmlTypeMapMember mem = (XmlTypeMapMember)members[n]; SetMemberValueFromAttr(mem, ob, mem.DefaultValue, isValueList); } } // Reads attributes XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember; int anyAttributeIndex = 0; object anyAttributeArray = null; while (Reader.MoveToNextAttribute()) { XmlTypeMapMemberAttribute member = map.GetAttribute(Reader.LocalName, Reader.NamespaceURI); if (member != null) { SetMemberValue(member, ob, GetValueFromXmlString(Reader.Value, member.TypeData, member.MappedType), isValueList); } else if (IsXmlnsAttribute(Reader.Name)) { // If the map has NamespaceDeclarations, // then store this xmlns to the given member. // If the instance doesn't exist, then create. if (map.NamespaceDeclarations != null) { XmlSerializerNamespaces nss = this.GetMemberValue(map.NamespaceDeclarations, ob, isValueList) as XmlSerializerNamespaces; if (nss == null) { nss = new XmlSerializerNamespaces(); SetMemberValue(map.NamespaceDeclarations, ob, nss, isValueList); } if (Reader.Prefix == "xmlns") { nss.Add(Reader.LocalName, Reader.Value); } else { nss.Add("", Reader.Value); } } } else if (anyAttrMember != null) { XmlAttribute attr = (XmlAttribute)Document.ReadNode(Reader); ParseWsdlArrayType(attr); AddListValue(anyAttrMember.TypeData, ref anyAttributeArray, anyAttributeIndex++, attr, true); } else { ProcessUnknownAttribute(ob); } } if (anyAttrMember != null) { anyAttributeArray = ShrinkArray((Array)anyAttributeArray, anyAttributeIndex, anyAttrMember.TypeData.Type.GetElementType(), true); SetMemberValue(anyAttrMember, ob, anyAttributeArray, isValueList); } if (!isValueList) { Reader.MoveToElement(); if (Reader.IsEmptyElement) { SetListMembersDefaults(map, ob, isValueList); return; } Reader.ReadStartElement(); } // Reads elements bool[] readFlag = new bool[(map.ElementMembers != null) ? map.ElementMembers.Count : 0]; bool hasAnyReturnMember = (isValueList && _format == SerializationFormat.Encoded && map.ReturnMember != null); Reader.MoveToContent(); int[] indexes = null; object[] flatLists = null; object[] flatListsChoices = null; Fixup fixup = null; int ind = 0; int maxInd; if (readByOrder) { if (map.ElementMembers != null) { maxInd = map.ElementMembers.Count; } else { maxInd = 0; } } else { maxInd = int.MaxValue; } if (map.FlatLists != null) { indexes = new int[map.FlatLists.Count]; flatLists = new object[map.FlatLists.Count]; foreach (XmlTypeMapMemberExpandable mem in map.FlatLists) { if (IsReadOnly(mem, mem.TypeData, ob, isValueList)) { flatLists[mem.FlatArrayIndex] = mem.GetValue(ob); } else if (mem.TypeData.Type.IsArray) { flatLists[mem.FlatArrayIndex] = InitializeList(mem.TypeData); } else { object list = mem.GetValue(ob); if (list == null) { list = InitializeList(mem.TypeData); SetMemberValue(mem, ob, list, isValueList); } flatLists[mem.FlatArrayIndex] = list; } if (mem.ChoiceMember != null) { if (flatListsChoices == null) { flatListsChoices = new object[map.FlatLists.Count]; } flatListsChoices[mem.FlatArrayIndex] = InitializeList(mem.ChoiceTypeData); } } } if (_format == SerializationFormat.Encoded && map.ElementMembers != null) { FixupCallbackInfo info = new FixupCallbackInfo(this, map, isValueList); fixup = new Fixup(ob, new XmlSerializationFixupCallback(info.FixupMembers), map.ElementMembers.Count); AddFixup(fixup); } while (Reader.NodeType != System.Xml.XmlNodeType.EndElement && (ind < maxInd)) { if (Reader.NodeType == System.Xml.XmlNodeType.Element) { XmlTypeMapElementInfo info; if (readByOrder) { info = map.GetElement(ind++); } else if (hasAnyReturnMember) { info = (XmlTypeMapElementInfo)((XmlTypeMapMemberElement)map.ReturnMember).ElementInfo[0]; hasAnyReturnMember = false; } else { info = map.GetElement(Reader.LocalName, Reader.NamespaceURI); } if (info != null && !readFlag[info.Member.Index]) { if (info.Member.GetType() == typeof(XmlTypeMapMemberList)) { if (_format == SerializationFormat.Encoded && info.MultiReferenceType) { object list = ReadReferencingElement(out fixup.Ids[info.Member.Index]); if (fixup.Ids[info.Member.Index] == null) // Already read { if (IsReadOnly(info.Member, info.TypeData, ob, isValueList)) { throw CreateReadOnlyCollectionException(info.TypeData.FullTypeName); } else { SetMemberValue(info.Member, ob, list, isValueList); } } else if (!info.MappedType.TypeData.Type.IsArray) { if (IsReadOnly(info.Member, info.TypeData, ob, isValueList)) { list = GetMemberValue(info.Member, ob, isValueList); } else { list = CreateList(info.MappedType.TypeData.Type); SetMemberValue(info.Member, ob, list, isValueList); } AddFixup(new CollectionFixup(list, new XmlSerializationCollectionFixupCallback(FillList), fixup.Ids[info.Member.Index])); fixup.Ids[info.Member.Index] = null; // The member already has the value, no further fix needed. } } else { if (IsReadOnly(info.Member, info.TypeData, ob, isValueList)) { ReadListElement(info.MappedType, info.IsNullable, GetMemberValue(info.Member, ob, isValueList), false); } else if (info.MappedType.TypeData.Type.IsArray) { object list = ReadListElement(info.MappedType, info.IsNullable, null, true); if (list != null || info.IsNullable) { SetMemberValue(info.Member, ob, list, isValueList); } } else { // If the member already has a list, reuse that list. No need to create a new one. object list = GetMemberValue(info.Member, ob, isValueList); if (list == null) { list = CreateList(info.MappedType.TypeData.Type); SetMemberValue(info.Member, ob, list, isValueList); } ReadListElement(info.MappedType, info.IsNullable, list, true); } } readFlag[info.Member.Index] = true; } else if (info.Member.GetType() == typeof(XmlTypeMapMemberFlatList)) { XmlTypeMapMemberFlatList mem = (XmlTypeMapMemberFlatList)info.Member; AddListValue(mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, ReadObjectElement(info), !IsReadOnly(info.Member, info.TypeData, ob, isValueList)); if (mem.ChoiceMember != null) { AddListValue(mem.ChoiceTypeData, ref flatListsChoices[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex] - 1, info.ChoiceValue, true); } } else if (info.Member.GetType() == typeof(XmlTypeMapMemberAnyElement)) { XmlTypeMapMemberAnyElement mem = (XmlTypeMapMemberAnyElement)info.Member; if (mem.TypeData.IsListType) { AddListValue(mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, ReadXmlNode(mem.TypeData.ListItemTypeData, false), true); } else { SetMemberValue(mem, ob, ReadXmlNode(mem.TypeData, false), isValueList); } } else if (info.Member.GetType() == typeof(XmlTypeMapMemberElement)) { object val; readFlag[info.Member.Index] = true; if (_format == SerializationFormat.Encoded) { if (info.Member.TypeData.SchemaType != SchemaTypes.Primitive) { val = ReadReferencingElement(out fixup.Ids[info.Member.Index]); } else { val = ReadReferencingElement(info.Member.TypeData.XmlType, System.Xml.Schema.XmlSchema.Namespace, out fixup.Ids[info.Member.Index]); } if (info.MultiReferenceType) { if (fixup.Ids[info.Member.Index] == null) // already read { SetMemberValue(info.Member, ob, val, isValueList); } } else if (val != null) { SetMemberValue(info.Member, ob, val, isValueList); } } else { SetMemberValue(info.Member, ob, ReadObjectElement(info), isValueList); if (info.ChoiceValue != null) { XmlTypeMapMemberElement imem = (XmlTypeMapMemberElement)info.Member; imem.SetChoice(ob, info.ChoiceValue); } } } else { throw new InvalidOperationException("Unknown member type"); } } else if (map.DefaultAnyElementMember != null) { XmlTypeMapMemberAnyElement mem = map.DefaultAnyElementMember; if (mem.TypeData.IsListType) { AddListValue(mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, ReadXmlNode(mem.TypeData.ListItemTypeData, false), true); } else { SetMemberValue(mem, ob, ReadXmlNode(mem.TypeData, false), isValueList); } } else { ProcessUnknownElement(ob); } } else if ((Reader.NodeType == System.Xml.XmlNodeType.Text || Reader.NodeType == System.Xml.XmlNodeType.CDATA) && map.XmlTextCollector != null) { if (map.XmlTextCollector is XmlTypeMapMemberExpandable) { XmlTypeMapMemberExpandable mem = (XmlTypeMapMemberExpandable)map.XmlTextCollector; XmlTypeMapMemberFlatList flatl = mem as XmlTypeMapMemberFlatList; TypeData itype = (flatl == null) ? mem.TypeData.ListItemTypeData : flatl.ListMap.FindTextElement().TypeData; object val = (itype.Type == typeof(string)) ? (object)Reader.ReadString() : (object)ReadXmlNode(itype, false); AddListValue(mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, val, true); } else { XmlTypeMapMemberElement mem = (XmlTypeMapMemberElement)map.XmlTextCollector; XmlTypeMapElementInfo info = (XmlTypeMapElementInfo)mem.ElementInfo[0]; if (info.TypeData.Type == typeof(string)) { SetMemberValue(mem, ob, ReadString((string)GetMemberValue(mem, ob, isValueList)), isValueList); } else { SetMemberValue(mem, ob, GetValueFromXmlString(Reader.ReadString(), info.TypeData, info.MappedType), isValueList); } } } else { UnknownNode(ob); } Reader.MoveToContent(); } if (flatLists != null) { foreach (XmlTypeMapMemberExpandable mem in map.FlatLists) { Object list = flatLists[mem.FlatArrayIndex]; if (mem.TypeData.Type.IsArray) { list = ShrinkArray((Array)list, indexes[mem.FlatArrayIndex], mem.TypeData.Type.GetElementType(), true); } if (!IsReadOnly(mem, mem.TypeData, ob, isValueList) && mem.TypeData.Type.IsArray) { SetMemberValue(mem, ob, list, isValueList); } } } if (flatListsChoices != null) { foreach (XmlTypeMapMemberExpandable mem in map.FlatLists) { Object list = flatListsChoices[mem.FlatArrayIndex]; if (list == null) { continue; } list = ShrinkArray((Array)list, indexes[mem.FlatArrayIndex], mem.ChoiceTypeData.Type.GetElementType(), true); XmlTypeMapMember.SetValue(ob, mem.ChoiceMember, list); } } SetListMembersDefaults(map, ob, isValueList); }
void ReadMembers(ClassMap map, object ob, bool isValueList, bool readBySoapOrder) { // Reads attributes ReadAttributeMembers(map, ob, isValueList); if (!isValueList) { Reader.MoveToElement(); if (Reader.IsEmptyElement) { SetListMembersDefaults(map, ob, isValueList); return; } Reader.ReadStartElement(); } // Reads elements bool[] readFlag = new bool[(map.ElementMembers != null) ? map.ElementMembers.Count : 0]; bool hasAnyReturnMember = (isValueList && _format == SerializationFormat.Encoded && map.ReturnMember != null); Reader.MoveToContent(); int[] indexes = null; object[] flatLists = null; object[] flatListsChoices = null; Fixup fixup = null; int ind = -1; int maxInd; if (readBySoapOrder) { if (map.ElementMembers != null) { maxInd = map.ElementMembers.Count; } else { maxInd = -1; } } else { maxInd = int.MaxValue; } if (map.FlatLists != null) { indexes = new int[map.FlatLists.Count]; flatLists = new object[map.FlatLists.Count]; foreach (XmlTypeMapMemberExpandable mem in map.FlatLists) { if (IsReadOnly(mem, mem.TypeData, ob, isValueList)) { flatLists [mem.FlatArrayIndex] = mem.GetValue(ob); } else if (mem.TypeData.Type.IsArray) { flatLists [mem.FlatArrayIndex] = InitializeList(mem.TypeData); } else { object list = mem.GetValue(ob); if (list == null) { list = InitializeList(mem.TypeData); SetMemberValue(mem, ob, list, isValueList); } flatLists [mem.FlatArrayIndex] = list; } if (mem.ChoiceMember != null) { if (flatListsChoices == null) { flatListsChoices = new object [map.FlatLists.Count]; } flatListsChoices [mem.FlatArrayIndex] = InitializeList(mem.ChoiceTypeData); } } } if (_format == SerializationFormat.Encoded && map.ElementMembers != null) { FixupCallbackInfo info = new FixupCallbackInfo(this, map, isValueList); fixup = new Fixup(ob, new XmlSerializationFixupCallback(info.FixupMembers), map.ElementMembers.Count); AddFixup(fixup); } XmlTypeMapMember previousMember = null; while (Reader.NodeType != System.Xml.XmlNodeType.EndElement && (ind < maxInd - 1)) { if (Reader.NodeType == System.Xml.XmlNodeType.Element) { XmlTypeMapElementInfo info; if (readBySoapOrder) { info = map.GetElement(Reader.LocalName, Reader.NamespaceURI, ind); } else if (hasAnyReturnMember) { info = (XmlTypeMapElementInfo)((XmlTypeMapMemberElement)map.ReturnMember).ElementInfo[0]; hasAnyReturnMember = false; } else { if (map.IsOrderDependentMap) { info = map.GetElement(Reader.LocalName, Reader.NamespaceURI, ind); } else { info = map.GetElement(Reader.LocalName, Reader.NamespaceURI); } } if (info != null && !readFlag[info.Member.Index]) { if (info.Member != previousMember) { ind = info.ExplicitOrder + 1; // If the member is a flat list don't increase the index, since the next element may // be another item of the list. This is a fix for Xamarin bug #9193. if (info.Member is XmlTypeMapMemberFlatList) { ind--; } previousMember = info.Member; } if (info.Member.GetType() == typeof(XmlTypeMapMemberList)) { if (_format == SerializationFormat.Encoded && info.MultiReferenceType) { object list = ReadReferencingElement(out fixup.Ids[info.Member.Index]); if (fixup.Ids[info.Member.Index] == null) // Already read { if (IsReadOnly(info.Member, info.TypeData, ob, isValueList)) { throw CreateReadOnlyCollectionException(info.TypeData.FullTypeName); } else { SetMemberValue(info.Member, ob, list, isValueList); } } else if (!info.MappedType.TypeData.Type.IsArray) { if (IsReadOnly(info.Member, info.TypeData, ob, isValueList)) { list = GetMemberValue(info.Member, ob, isValueList); } else { list = CreateList(info.MappedType.TypeData.Type); SetMemberValue(info.Member, ob, list, isValueList); } AddFixup(new CollectionFixup(list, new XmlSerializationCollectionFixupCallback(FillList), fixup.Ids[info.Member.Index])); fixup.Ids[info.Member.Index] = null; // The member already has the value, no further fix needed. } } else { if (IsReadOnly(info.Member, info.TypeData, ob, isValueList)) { ReadListElement(info.MappedType, info.IsNullable, GetMemberValue(info.Member, ob, isValueList), false); } else if (info.MappedType.TypeData.Type.IsArray) { object list = ReadListElement(info.MappedType, info.IsNullable, null, true); if (list != null || info.IsNullable) { SetMemberValue(info.Member, ob, list, isValueList); } } else { // If the member already has a list, reuse that list. No need to create a new one. object list = GetMemberValue(info.Member, ob, isValueList); if (list == null) { list = CreateList(info.MappedType.TypeData.Type); SetMemberValue(info.Member, ob, list, isValueList); } ReadListElement(info.MappedType, info.IsNullable, list, true); } } readFlag[info.Member.Index] = true; } else if (info.Member.GetType() == typeof(XmlTypeMapMemberFlatList)) { XmlTypeMapMemberFlatList mem = (XmlTypeMapMemberFlatList)info.Member; AddListValue(mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, ReadObjectElement(info), !IsReadOnly(info.Member, info.TypeData, ob, isValueList)); if (mem.ChoiceMember != null) { AddListValue(mem.ChoiceTypeData, ref flatListsChoices [mem.FlatArrayIndex], indexes[mem.FlatArrayIndex] - 1, info.ChoiceValue, true); } } else if (info.Member.GetType() == typeof(XmlTypeMapMemberAnyElement)) { XmlTypeMapMemberAnyElement mem = (XmlTypeMapMemberAnyElement)info.Member; if (mem.TypeData.IsListType) { AddListValue(mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, ReadXmlNode(mem.TypeData.ListItemTypeData, false), true); } else { SetMemberValue(mem, ob, ReadXmlNode(mem.TypeData, false), isValueList); } } else if (info.Member.GetType() == typeof(XmlTypeMapMemberElement)) { object val; readFlag[info.Member.Index] = true; if (_format == SerializationFormat.Encoded) { if (info.Member.TypeData.SchemaType != SchemaTypes.Primitive) { val = ReadReferencingElement(out fixup.Ids[info.Member.Index]); } else { val = ReadReferencingElement(info.Member.TypeData.XmlType, System.Xml.Schema.XmlSchema.Namespace, out fixup.Ids[info.Member.Index]); } if (info.MultiReferenceType) { if (fixup.Ids[info.Member.Index] == null) // already read { SetMemberValue(info.Member, ob, val, isValueList); } } else if (val != null) { SetMemberValue(info.Member, ob, val, isValueList); } } else { SetMemberValue(info.Member, ob, ReadObjectElement(info), isValueList); if (info.ChoiceValue != null) { XmlTypeMapMemberElement imem = (XmlTypeMapMemberElement)info.Member; imem.SetChoice(ob, info.ChoiceValue); } } } else { throw new InvalidOperationException("Unknown member type"); } } else if (map.DefaultAnyElementMember != null) { XmlTypeMapMemberAnyElement mem = map.DefaultAnyElementMember; if (mem.TypeData.IsListType) { AddListValue(mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, ReadXmlNode(mem.TypeData.ListItemTypeData, false), true); } else { SetMemberValue(mem, ob, ReadXmlNode(mem.TypeData, false), isValueList); } } else { ProcessUnknownElement(ob); } } else if ((Reader.NodeType == System.Xml.XmlNodeType.Text || Reader.NodeType == System.Xml.XmlNodeType.CDATA) && map.XmlTextCollector != null) { if (map.XmlTextCollector is XmlTypeMapMemberExpandable) { XmlTypeMapMemberExpandable mem = (XmlTypeMapMemberExpandable)map.XmlTextCollector; XmlTypeMapMemberFlatList flatl = mem as XmlTypeMapMemberFlatList; TypeData itype = (flatl == null) ? mem.TypeData.ListItemTypeData : flatl.ListMap.FindTextElement().TypeData; object val = (itype.Type == typeof(string)) ? (object)Reader.ReadString() : (object)ReadXmlNode(itype, false); AddListValue(mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, val, true); } else { XmlTypeMapMemberElement mem = (XmlTypeMapMemberElement)map.XmlTextCollector; XmlTypeMapElementInfo info = (XmlTypeMapElementInfo)mem.ElementInfo [0]; if (info.TypeData.Type == typeof(string)) { SetMemberValue(mem, ob, ReadString((string)GetMemberValue(mem, ob, isValueList)), isValueList); } else { SetMemberValue(mem, ob, GetValueFromXmlString(Reader.ReadString(), info.TypeData, info.MappedType), isValueList); } } } else { UnknownNode(ob); } Reader.MoveToContent(); } if (flatLists != null) { foreach (XmlTypeMapMemberExpandable mem in map.FlatLists) { Object list = flatLists[mem.FlatArrayIndex]; if (mem.TypeData.Type.IsArray) { list = ShrinkArray((Array)list, indexes[mem.FlatArrayIndex], mem.TypeData.Type.GetElementType(), true); } if (!IsReadOnly(mem, mem.TypeData, ob, isValueList) && mem.TypeData.Type.IsArray) { SetMemberValue(mem, ob, list, isValueList); } } } if (flatListsChoices != null) { foreach (XmlTypeMapMemberExpandable mem in map.FlatLists) { Object list = flatListsChoices[mem.FlatArrayIndex]; if (list == null) { continue; } list = ShrinkArray((Array)list, indexes[mem.FlatArrayIndex], mem.ChoiceTypeData.Type.GetElementType(), true); XmlTypeMapMember.SetValue(ob, mem.ChoiceMember, list); } } SetListMembersDefaults(map, ob, isValueList); }
void WriteElementMembers (ClassMap map, object ob, bool isValueList) { ICollection members = map.ElementMembers; if (members != null) { foreach (XmlTypeMapMemberElement member in members) { if (!MemberHasValue (member, ob, isValueList)) continue; object memberValue = GetMemberValue (member, ob, isValueList); Type memType = member.GetType(); if (memType == typeof(XmlTypeMapMemberList)) { WriteMemberElement ((XmlTypeMapElementInfo) member.ElementInfo[0], memberValue); } else if (memType == typeof(XmlTypeMapMemberFlatList)) { if (memberValue != null) WriteListContent (ob, member.TypeData, ((XmlTypeMapMemberFlatList)member).ListMap, memberValue, null); } else if (memType == typeof(XmlTypeMapMemberAnyElement)) { if (memberValue != null) WriteAnyElementContent ((XmlTypeMapMemberAnyElement)member, memberValue); } else if (memType == typeof(XmlTypeMapMemberAnyAttribute)) { // Ignore } else if (memType == typeof(XmlTypeMapMemberElement)) { XmlTypeMapElementInfo elem = member.FindElement (ob, memberValue); WriteMemberElement (elem, memberValue); } else throw new InvalidOperationException ("Unknown member type"); } } }
void ReadAttributeMembers (ClassMap map, object ob, bool isValueList) { XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember; int anyAttributeIndex = 0; object anyAttributeArray = null; while (Reader.MoveToNextAttribute()) { XmlTypeMapMemberAttribute member = map.GetAttribute (Reader.LocalName, Reader.NamespaceURI); if (member != null) { SetMemberValue (member, ob, GetValueFromXmlString (Reader.Value, member.TypeData, member.MappedType), isValueList); } else if (IsXmlnsAttribute(Reader.Name)) { // If the map has NamespaceDeclarations, // then store this xmlns to the given member. // If the instance doesn't exist, then create. if (map.NamespaceDeclarations != null) { XmlSerializerNamespaces nss = this.GetMemberValue (map.NamespaceDeclarations, ob, isValueList) as XmlSerializerNamespaces; if (nss == null) { nss = new XmlSerializerNamespaces (); SetMemberValue (map.NamespaceDeclarations, ob, nss, isValueList); } if (Reader.Prefix == "xmlns") nss.Add (Reader.LocalName, Reader.Value); else nss.Add ("", Reader.Value); } } else if (anyAttrMember != null) { XmlAttribute attr = (XmlAttribute) Document.ReadNode(Reader); ParseWsdlArrayType (attr); AddListValue (anyAttrMember.TypeData, ref anyAttributeArray, anyAttributeIndex++, attr, true); } else ProcessUnknownAttribute(ob); } if (anyAttrMember != null) { anyAttributeArray = ShrinkArray ((Array)anyAttributeArray, anyAttributeIndex, anyAttrMember.TypeData.Type.GetElementType(), true); SetMemberValue (anyAttrMember, ob, anyAttributeArray, isValueList); } Reader.MoveToElement (); }
void ExportMembersMapCode(CodeTypeDeclaration codeClass, ClassMap map, string defaultNamespace, XmlTypeMapping baseMap) { ICollection attributes = map.AttributeMembers; ICollection members = map.ElementMembers; // collect names if (attributes != null) { foreach (XmlTypeMapMemberAttribute attr in attributes) { identifiers.AddUnique(attr.Name, attr); } } if (members != null) { foreach (XmlTypeMapMemberElement member in members) { identifiers.AddUnique(member.Name, member); } } // Write attributes if (attributes != null) { foreach (XmlTypeMapMemberAttribute attr in attributes) { if (baseMap != null && DefinedInBaseMap(baseMap, attr)) { continue; } AddAttributeFieldMember(codeClass, attr, defaultNamespace); } } members = map.ElementMembers; if (members != null) { foreach (XmlTypeMapMemberElement member in members) { if (baseMap != null && DefinedInBaseMap(baseMap, member)) { continue; } Type memType = member.GetType(); if (memType == typeof(XmlTypeMapMemberList)) { AddArrayElementFieldMember(codeClass, (XmlTypeMapMemberList)member, defaultNamespace); } else if (memType == typeof(XmlTypeMapMemberFlatList)) { AddElementFieldMember(codeClass, member, defaultNamespace); } else if (memType == typeof(XmlTypeMapMemberAnyElement)) { AddAnyElementFieldMember(codeClass, member, defaultNamespace); } else if (memType == typeof(XmlTypeMapMemberElement)) { AddElementFieldMember(codeClass, member, defaultNamespace); } else { throw new InvalidOperationException("Member type " + memType + " not supported"); } } } XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember; if (anyAttrMember != null) { CodeTypeMember codeField = CreateFieldMember(codeClass, anyAttrMember.TypeData, anyAttrMember.Name); AddComments(codeField, anyAttrMember.Documentation); codeField.Attributes = MemberAttributes.Public; GenerateAnyAttribute(codeField); } }
void ReadMembers (ClassMap map, object ob, bool isValueList, bool readByOrder) { // Reads attributes ReadAttributeMembers (map, ob, isValueList); if (!isValueList) { Reader.MoveToElement(); if (Reader.IsEmptyElement) { SetListMembersDefaults (map, ob, isValueList); return; } Reader.ReadStartElement(); } // Reads elements bool[] readFlag = new bool[(map.ElementMembers != null) ? map.ElementMembers.Count : 0]; bool hasAnyReturnMember = (isValueList && _format == SerializationFormat.Encoded && map.ReturnMember != null); Reader.MoveToContent(); int[] indexes = null; object[] flatLists = null; object[] flatListsChoices = null; Fixup fixup = null; int ind = 0; int maxInd; if (readByOrder) { if (map.ElementMembers != null) maxInd = map.ElementMembers.Count; else maxInd = 0; } else maxInd = int.MaxValue; if (map.FlatLists != null) { indexes = new int[map.FlatLists.Count]; flatLists = new object[map.FlatLists.Count]; foreach (XmlTypeMapMemberExpandable mem in map.FlatLists) { if (IsReadOnly (mem, mem.TypeData, ob, isValueList)) flatLists [mem.FlatArrayIndex] = mem.GetValue (ob); else if (mem.TypeData.Type.IsArray) { flatLists [mem.FlatArrayIndex] = InitializeList (mem.TypeData); } else { object list = mem.GetValue (ob); if (list == null) { list = InitializeList (mem.TypeData); SetMemberValue (mem, ob, list, isValueList); } flatLists [mem.FlatArrayIndex] = list; } if (mem.ChoiceMember != null) { if (flatListsChoices == null) flatListsChoices = new object [map.FlatLists.Count]; flatListsChoices [mem.FlatArrayIndex] = InitializeList (mem.ChoiceTypeData); } } } if (_format == SerializationFormat.Encoded && map.ElementMembers != null) { FixupCallbackInfo info = new FixupCallbackInfo (this, map, isValueList); fixup = new Fixup(ob, new XmlSerializationFixupCallback(info.FixupMembers), map.ElementMembers.Count); AddFixup (fixup); } while (Reader.NodeType != System.Xml.XmlNodeType.EndElement && (ind < maxInd)) { if (Reader.NodeType == System.Xml.XmlNodeType.Element) { XmlTypeMapElementInfo info; if (readByOrder) { info = map.GetElement (ind++); } else if (hasAnyReturnMember) { info = (XmlTypeMapElementInfo) ((XmlTypeMapMemberElement)map.ReturnMember).ElementInfo[0]; hasAnyReturnMember = false; } else info = map.GetElement (Reader.LocalName, Reader.NamespaceURI); if (info != null && !readFlag[info.Member.Index] ) { if (info.Member.GetType() == typeof (XmlTypeMapMemberList)) { if (_format == SerializationFormat.Encoded && info.MultiReferenceType) { object list = ReadReferencingElement (out fixup.Ids[info.Member.Index]); if (fixup.Ids[info.Member.Index] == null) // Already read { if (IsReadOnly (info.Member, info.TypeData, ob, isValueList)) throw CreateReadOnlyCollectionException (info.TypeData.FullTypeName); else SetMemberValue (info.Member, ob, list, isValueList); } else if (!info.MappedType.TypeData.Type.IsArray) { if (IsReadOnly (info.Member, info.TypeData, ob, isValueList)) list = GetMemberValue (info.Member, ob, isValueList); else { list = CreateList (info.MappedType.TypeData.Type); SetMemberValue (info.Member, ob, list, isValueList); } AddFixup (new CollectionFixup (list, new XmlSerializationCollectionFixupCallback (FillList), fixup.Ids[info.Member.Index])); fixup.Ids[info.Member.Index] = null; // The member already has the value, no further fix needed. } } else { if (IsReadOnly (info.Member, info.TypeData, ob, isValueList)) { ReadListElement (info.MappedType, info.IsNullable, GetMemberValue (info.Member, ob, isValueList), false); } else if (info.MappedType.TypeData.Type.IsArray) { object list = ReadListElement (info.MappedType, info.IsNullable, null, true); if (list != null || info.IsNullable) SetMemberValue (info.Member, ob, list, isValueList); } else { // If the member already has a list, reuse that list. No need to create a new one. object list = GetMemberValue (info.Member, ob, isValueList); if (list == null) { list = CreateList (info.MappedType.TypeData.Type); SetMemberValue (info.Member, ob, list, isValueList); } ReadListElement (info.MappedType, info.IsNullable, list, true); } } readFlag[info.Member.Index] = true; } else if (info.Member.GetType() == typeof (XmlTypeMapMemberFlatList)) { XmlTypeMapMemberFlatList mem = (XmlTypeMapMemberFlatList)info.Member; AddListValue (mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, ReadObjectElement (info), !IsReadOnly (info.Member, info.TypeData, ob, isValueList)); if (mem.ChoiceMember != null) { AddListValue (mem.ChoiceTypeData, ref flatListsChoices [mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]-1, info.ChoiceValue, true); } } else if (info.Member.GetType() == typeof (XmlTypeMapMemberAnyElement)) { XmlTypeMapMemberAnyElement mem = (XmlTypeMapMemberAnyElement)info.Member; if (mem.TypeData.IsListType) AddListValue (mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, ReadXmlNode (mem.TypeData.ListItemTypeData, false), true); else SetMemberValue (mem, ob, ReadXmlNode (mem.TypeData, false), isValueList); } else if (info.Member.GetType() == typeof(XmlTypeMapMemberElement)) { object val; readFlag[info.Member.Index] = true; if (_format == SerializationFormat.Encoded) { if (info.Member.TypeData.SchemaType != SchemaTypes.Primitive) val = ReadReferencingElement (out fixup.Ids[info.Member.Index]); else val = ReadReferencingElement (info.Member.TypeData.XmlType, System.Xml.Schema.XmlSchema.Namespace, out fixup.Ids[info.Member.Index]); if (info.MultiReferenceType) { if (fixup.Ids[info.Member.Index] == null) // already read SetMemberValue (info.Member, ob, val, isValueList); } else if (val != null) SetMemberValue (info.Member, ob, val, isValueList); } else { SetMemberValue (info.Member, ob, ReadObjectElement (info), isValueList); if (info.ChoiceValue != null) { XmlTypeMapMemberElement imem = (XmlTypeMapMemberElement) info.Member; imem.SetChoice (ob, info.ChoiceValue); } } } else throw new InvalidOperationException ("Unknown member type"); } else if (map.DefaultAnyElementMember != null) { XmlTypeMapMemberAnyElement mem = map.DefaultAnyElementMember; if (mem.TypeData.IsListType) AddListValue (mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, ReadXmlNode (mem.TypeData.ListItemTypeData, false), true); else SetMemberValue (mem, ob, ReadXmlNode (mem.TypeData, false), isValueList); } else ProcessUnknownElement(ob); } else if ((Reader.NodeType == System.Xml.XmlNodeType.Text || Reader.NodeType == System.Xml.XmlNodeType.CDATA) && map.XmlTextCollector != null) { if (map.XmlTextCollector is XmlTypeMapMemberExpandable) { XmlTypeMapMemberExpandable mem = (XmlTypeMapMemberExpandable)map.XmlTextCollector; XmlTypeMapMemberFlatList flatl = mem as XmlTypeMapMemberFlatList; TypeData itype = (flatl == null) ? mem.TypeData.ListItemTypeData : flatl.ListMap.FindTextElement().TypeData; object val = (itype.Type == typeof (string)) ? (object) Reader.ReadString() : (object) ReadXmlNode (itype, false); AddListValue (mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, val, true); } else { XmlTypeMapMemberElement mem = (XmlTypeMapMemberElement) map.XmlTextCollector; XmlTypeMapElementInfo info = (XmlTypeMapElementInfo) mem.ElementInfo [0]; if (info.TypeData.Type == typeof (string)) SetMemberValue (mem, ob, ReadString ((string) GetMemberValue (mem, ob, isValueList)), isValueList); else SetMemberValue (mem, ob, GetValueFromXmlString (Reader.ReadString(), info.TypeData, info.MappedType), isValueList); } } else UnknownNode(ob); Reader.MoveToContent(); } if (flatLists != null) { foreach (XmlTypeMapMemberExpandable mem in map.FlatLists) { Object list = flatLists[mem.FlatArrayIndex]; if (mem.TypeData.Type.IsArray) list = ShrinkArray ((Array)list, indexes[mem.FlatArrayIndex], mem.TypeData.Type.GetElementType(), true); if (!IsReadOnly (mem, mem.TypeData, ob, isValueList) && mem.TypeData.Type.IsArray) SetMemberValue (mem, ob, list, isValueList); } } if (flatListsChoices != null) { foreach (XmlTypeMapMemberExpandable mem in map.FlatLists) { Object list = flatListsChoices[mem.FlatArrayIndex]; if (list == null) continue; list = ShrinkArray ((Array)list, indexes[mem.FlatArrayIndex], mem.ChoiceTypeData.Type.GetElementType(), true); XmlTypeMapMember.SetValue (ob, mem.ChoiceMember, list); } } SetListMembersDefaults (map, ob, isValueList); }
bool HasMixedContent(XmlTypeMapping map) { ClassMap cmap = (ClassMap)map.ObjectMap; return(cmap.XmlTextCollector != null && (map.BaseMap == null || !DefinedInBaseMap(map.BaseMap, cmap.XmlTextCollector))); }
void SetListMembersDefaults (ClassMap map, object ob, bool isValueList) { if (map.ListMembers != null) { ArrayList members = map.ListMembers; for (int n=0; n<members.Count; n++) { XmlTypeMapMember mem = (XmlTypeMapMember) members[n]; if (IsReadOnly (mem, mem.TypeData, ob, isValueList)) continue; if (GetMemberValue (mem, ob, isValueList) == null) SetMemberValue (mem, ob, InitializeList (mem.TypeData), isValueList); } } }
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(); }
internal void FixupMembers (ClassMap map, object obfixup, bool isValueList) { Fixup fixup = (Fixup)obfixup; ICollection members = map.ElementMembers; string[] ids = fixup.Ids; foreach (XmlTypeMapMember member in members) { if (ids[member.Index] != null) SetMemberValue (member, fixup.Source, GetTarget(ids[member.Index]), isValueList); } }
void GenerateReadAttributeMembers (XmlMapping xmlMap, ClassMap map, string ob, bool isValueList, ref bool first) { XmlTypeMapping typeMap = xmlMap as XmlTypeMapping; Type xmlMapType = (typeMap != null) ? typeMap.TypeData.Type : typeof(object[]); if (GenerateReadHook (HookType.attributes, xmlMapType)) return; XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember; if (anyAttrMember != null) { WriteLine ("int anyAttributeIndex = 0;"); WriteLine (anyAttrMember.TypeData.CSharpFullName + " anyAttributeArray = null;"); } WriteLine ("while (Reader.MoveToNextAttribute())"); WriteLineInd ("{"); first = true; if (map.AttributeMembers != null) { foreach (XmlTypeMapMemberAttribute at in map.AttributeMembers) { WriteLineInd ((first?"":"else ") + "if (Reader.LocalName == " + GetLiteral (at.AttributeName) + " && Reader.NamespaceURI == " + GetLiteral (at.Namespace) + ") {"); if (!GenerateReadMemberHook (xmlMapType, at)) { GenerateSetMemberValue (at, ob, GenerateGetValueFromXmlString ("Reader.Value", at.TypeData, at.MappedType, false), isValueList); GenerateEndHook (); } WriteLineUni ("}"); first = false; } } WriteLineInd ((first?"":"else ") + "if (IsXmlnsAttribute (Reader.Name)) {"); // If the map has NamespaceDeclarations, // then store this xmlns to the given member. // If the instance doesn't exist, then create. if (map.NamespaceDeclarations != null) { if (!GenerateReadMemberHook (xmlMapType, map.NamespaceDeclarations)) { string nss = ob + ".@" + map.NamespaceDeclarations.Name; WriteLine ("if (" + nss + " == null) " + nss + " = new XmlSerializerNamespaces ();"); WriteLineInd ("if (Reader.Prefix == \"xmlns\")"); WriteLine (nss + ".Add (Reader.LocalName, Reader.Value);"); Unindent (); WriteLineInd ("else"); WriteLine (nss + ".Add (\"\", Reader.Value);"); Unindent (); GenerateEndHook (); } } WriteLineUni ("}"); WriteLineInd ("else {"); if (anyAttrMember != null) { if (!GenerateReadArrayMemberHook (xmlMapType, anyAttrMember, "anyAttributeIndex")) { WriteLine ("System.Xml.XmlAttribute attr = (System.Xml.XmlAttribute) Document.ReadNode(Reader);"); if (typeof(System.Xml.Schema.XmlSchemaAnnotated).IsAssignableFrom (xmlMapType)) WriteLine ("ParseWsdlArrayType (attr);"); GenerateAddListValue (anyAttrMember.TypeData, "anyAttributeArray", "anyAttributeIndex", GetCast (anyAttrMember.TypeData.ListItemTypeData, "attr"), true); GenerateEndHook (); } WriteLine ("anyAttributeIndex++;"); } else { if (!GenerateReadHook (HookType.unknownAttribute, xmlMapType)) { WriteLine ("UnknownNode (" + ob + ");"); GenerateEndHook (); } } WriteLineUni ("}"); WriteLineUni ("}"); if (anyAttrMember != null && !MemberHasReadReplaceHook (xmlMapType, anyAttrMember)) { WriteLine (""); WriteLine("anyAttributeArray = (" + anyAttrMember.TypeData.CSharpFullName + ") ShrinkArray (anyAttributeArray, anyAttributeIndex, " + GetTypeOf(anyAttrMember.TypeData.Type.GetElementType()) + ", true);"); GenerateSetMemberValue (anyAttrMember, ob, "anyAttributeArray", isValueList); } WriteLine (""); WriteLine ("Reader.MoveToElement ();"); GenerateEndHook (); }
public FixupCallbackInfo (XmlSerializationReaderInterpreter sri, ClassMap map, bool isValueList) { _sri = sri; _map = map; _isValueList = isValueList; }
void ImportChoiceContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaChoice choice, CodeIdentifiers classIds, bool multiValue) { XmlTypeMapElementInfoList choices = new XmlTypeMapElementInfoList (); multiValue = ImportChoices (typeQName, null, choices, choice.Items) || multiValue; if (choices.Count == 0) return; if (choice.MaxOccurs > 1) multiValue = true; XmlTypeMapMemberElement member; if (multiValue) { member = new XmlTypeMapMemberFlatList (); member.Name = classIds.AddUnique ("Items", member); ListMap listMap = new ListMap (); listMap.ItemInfo = choices; ((XmlTypeMapMemberFlatList)member).ListMap = listMap; } else { member = new XmlTypeMapMemberElement (); member.Name = classIds.AddUnique ("Item", member); } // If all choices have the same type, use that type for the member. // If not use System.Object. // If there are at least two choices with the same type, use a choice // identifier attribute TypeData typeData = null; bool twoEqual = false; bool allEqual = true; Hashtable types = new Hashtable (); for (int n = choices.Count - 1; n >= 0; n--) { XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo) choices [n]; // In some complex schemas, we may end up with several options // with the same name. It is better to ignore the extra options // than to crash. It's the best we can do, and btw it works // better than in MS.NET. if (cmap.GetElement (einfo.ElementName, einfo.Namespace, einfo.ExplicitOrder) != null || choices.IndexOfElement (einfo.ElementName, einfo.Namespace) != n) { choices.RemoveAt (n); continue; } if (types.ContainsKey (einfo.TypeData)) twoEqual = true; else types.Add (einfo.TypeData, einfo); TypeData choiceType = einfo.TypeData; if (choiceType.SchemaType == SchemaTypes.Class) { // When comparing class types, use the most generic class in the // inheritance hierarchy XmlTypeMapping choiceMap = GetTypeMapping (choiceType); BuildPendingMap (choiceMap); while (choiceMap.BaseMap != null) { choiceMap = choiceMap.BaseMap; BuildPendingMap (choiceMap); choiceType = choiceMap.TypeData; } } if (typeData == null) typeData = choiceType; else if (typeData != choiceType) allEqual = false; } if (!allEqual) typeData = TypeTranslator.GetTypeData (typeof(object)); if (twoEqual) { // Create the choice member XmlTypeMapMemberElement choiceMember = new XmlTypeMapMemberElement (); choiceMember.Ignore = true; choiceMember.Name = classIds.AddUnique (member.Name + "ElementName", choiceMember); member.ChoiceMember = choiceMember.Name; // Create the choice enum XmlTypeMapping enumMap = CreateTypeMapping (new XmlQualifiedName (member.Name + "ChoiceType", typeQName.Namespace), SchemaTypes.Enum, null); enumMap.IncludeInSchema = false; CodeIdentifiers codeIdents = new CodeIdentifiers (); EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember [choices.Count]; for (int n=0; n<choices.Count; n++) { XmlTypeMapElementInfo it =(XmlTypeMapElementInfo) choices[n]; bool extraNs = (it.Namespace != null && it.Namespace != "" && it.Namespace != typeQName.Namespace); string xmlName = extraNs ? it.Namespace + ":" + it.ElementName : it.ElementName; string enumName = codeIdents.AddUnique (CodeIdentifier.MakeValid (it.ElementName), it); members [n] = new EnumMap.EnumMapMember (xmlName, enumName); } enumMap.ObjectMap = new EnumMap (members, false); choiceMember.TypeData = multiValue ? enumMap.TypeData.ListTypeData : enumMap.TypeData; choiceMember.ElementInfo.Add (CreateElementInfo (typeQName.Namespace, choiceMember, choiceMember.Name, choiceMember.TypeData, false, XmlSchemaForm.None, -1)); cmap.AddMember (choiceMember); } if (typeData == null) return; if (multiValue) typeData = typeData.ListTypeData; member.ElementInfo = choices; member.Documentation = GetDocumentation (choice); member.TypeData = typeData; cmap.AddMember (member); }
void ExportMembersMapSchema (XmlSchema schema, ClassMap map, XmlTypeMapping baseMap, XmlSchemaObjectCollection outAttributes, out XmlSchemaSequence particle, out XmlSchemaAnyAttribute anyAttribute) { particle = null; XmlSchemaSequence seq = new XmlSchemaSequence (); ICollection members = map.ElementMembers; if (members != null && !map.HasSimpleContent) { foreach (XmlTypeMapMemberElement member in members) { if (baseMap != null && DefinedInBaseMap (baseMap, member)) continue; Type memType = member.GetType(); if (memType == typeof(XmlTypeMapMemberFlatList)) { XmlSchemaParticle part = GetSchemaArrayElement (schema, member.ElementInfo); if (part != null) seq.Items.Add (part); } else if (memType == typeof(XmlTypeMapMemberAnyElement)) { seq.Items.Add (GetSchemaArrayElement (schema, member.ElementInfo)); } else if (memType == typeof(XmlTypeMapMemberElement)) { GetSchemaElement (schema, (XmlTypeMapElementInfo) member.ElementInfo [0], member.DefaultValue, true, new XmlSchemaObjectContainer (seq)); } else { GetSchemaElement (schema, (XmlTypeMapElementInfo) member.ElementInfo[0], true, new XmlSchemaObjectContainer (seq)); } } } if (seq.Items.Count > 0) particle = seq; // Write attributes ICollection attributes = map.AttributeMembers; if (attributes != null) { foreach (XmlTypeMapMemberAttribute attr in attributes) { if (baseMap != null && DefinedInBaseMap (baseMap, attr)) continue; outAttributes.Add (GetSchemaAttribute (schema, attr, true)); } } XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember; if (anyAttrMember != null) anyAttribute = new XmlSchemaAnyAttribute (); else anyAttribute = null; }
public XmlTypeMapping ImportDerivedTypeMapping (XmlQualifiedName name, Type baseType, bool baseTypeCanBeIndirect) { XmlQualifiedName qname; XmlSchemaType stype; if (encodedFormat) { qname = name; stype = schemas.Find (name, typeof (XmlSchemaComplexType)) as XmlSchemaComplexType; if (stype == null) throw new InvalidOperationException ("Schema type '" + name + "' not found or not valid"); } else { if (!LocateElement (name, out qname, out stype)) return null; } XmlTypeMapping map = GetRegisteredTypeMapping (qname, baseType); if (map != null) { // If the type has already been imported, make sure that the map // has the requested base type SetMapBaseType (map, baseType); map.UpdateRoot (name); return map; } map = CreateTypeMapping (qname, SchemaTypes.Class, name); if (stype != null) { map.Documentation = GetDocumentation (stype); RegisterMapFixup (map, qname, (XmlSchemaComplexType)stype); } else { ClassMap cmap = new ClassMap (); CodeIdentifiers classIds = new CodeIdentifiers (); map.ObjectMap = cmap; AddTextMember (qname, cmap, classIds); } BuildPendingMaps (); SetMapBaseType (map, baseType); return map; }
XmlTypeMapping ImportClassMapping(Type type, XmlRootAttribute root, string defaultNamespace) { TypeData typeData = TypeTranslator.GetTypeData(type); XmlTypeMapping map = helper.GetRegisteredClrType(type, GetTypeNamespace(typeData, root, defaultNamespace)); if (map != null) { return(map); } if (!allowPrivateTypes) { ReflectionHelper.CheckSerializableType(type); } map = CreateTypeMapping(typeData, root, null, defaultNamespace); helper.RegisterClrType(map, type, map.XmlTypeNamespace); helper.RegisterSchemaType(map, map.XmlType, map.XmlTypeNamespace); // Import members ClassMap classMap = new ClassMap(); map.ObjectMap = classMap; // try // { ICollection members = GetReflectionMembers(type); foreach (XmlReflectionMember rmember in members) { if (rmember.XmlAttributes.XmlIgnore) { continue; } XmlTypeMapMember mem = CreateMapMember(rmember, map.XmlTypeNamespace); mem.CheckOptionalValueType(type); classMap.AddMember(mem); } // } // catch (Exception ex) { // throw helper.CreateError (map, ex.Message); // } ImportIncludedTypes(type, defaultNamespace); // Import extra classes if (type == typeof(object) && includedTypes != null) { foreach (Type intype in includedTypes) { map.DerivedTypes.Add(ImportTypeMapping(intype, defaultNamespace)); } } // Register inheritance relations if (type.BaseType != null) { XmlTypeMapping bmap = ImportClassMapping(type.BaseType, root, defaultNamespace); if (type.BaseType != typeof(object)) { map.BaseMap = bmap; classMap.SetCanBeSimpleType(false); } // At this point, derived classes of this map must be already registered RegisterDerivedMap(bmap, map); if (((ClassMap)bmap.ObjectMap).HasSimpleContent && classMap.ElementMembers != null && classMap.ElementMembers.Count != 1) { throw new InvalidOperationException(String.Format(errSimple, map.TypeData.TypeName, map.BaseMap.TypeData.TypeName)); } } if (classMap.XmlTextCollector != null && !classMap.HasSimpleContent) { XmlTypeMapMember mem = classMap.XmlTextCollector; if (mem.TypeData.Type != typeof(string) && mem.TypeData.Type != typeof(string[]) && mem.TypeData.Type != typeof(object[]) && mem.TypeData.Type != typeof(XmlNode[])) { throw new InvalidOperationException(String.Format(errSimple2, map.TypeData.TypeName, mem.Name, mem.TypeData.TypeName)); } } return(map); }
void BuildClassMap (XmlTypeMapping map, XmlQualifiedName typeQName, XmlSchemaComplexType stype) { CodeIdentifiers classIds = new CodeIdentifiers(); classIds.AddReserved (map.TypeData.TypeName); ClassMap cmap = new ClassMap (); map.ObjectMap = cmap; bool isMixed = stype.IsMixed; if (stype.Particle != null) ImportParticleComplexContent (typeQName, cmap, stype.Particle, classIds, isMixed); else { if (stype.ContentModel is XmlSchemaSimpleContent) { ImportSimpleContent (typeQName, map, (XmlSchemaSimpleContent)stype.ContentModel, classIds, isMixed); } else if (stype.ContentModel is XmlSchemaComplexContent) { ImportComplexContent (typeQName, map, (XmlSchemaComplexContent)stype.ContentModel, classIds, isMixed); } } ImportAttributes (typeQName, cmap, stype.Attributes, stype.AnyAttribute, classIds); ImportExtensionTypes (typeQName); if (isMixed) AddTextMember (typeQName, cmap, classIds); AddObjectDerivedMap (map); }
XmlTypeMapping ImportClassMapping (TypeData typeData, XmlRootAttribute root, string defaultNamespace) { Type type = typeData.Type; XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace)); if (map != null) return map; if (!allowPrivateTypes) ReflectionHelper.CheckSerializableType (type, false); map = CreateTypeMapping (typeData, root, null, defaultNamespace); helper.RegisterClrType (map, type, map.XmlTypeNamespace); helper.RegisterSchemaType (map, map.XmlType, map.XmlTypeNamespace); // Import members ClassMap classMap = new ClassMap (); map.ObjectMap = classMap; var members = GetReflectionMembers (type); bool? isOrderExplicit = null; foreach (XmlReflectionMember rmember in members) { int? order = rmember.XmlAttributes.Order; if (isOrderExplicit == null) { if (order != null) isOrderExplicit = (int) order >= 0; } else if (order != null && isOrderExplicit != ((int) order >= 0)) throw new InvalidOperationException ("Inconsistent XML sequence was detected. If there are XmlElement/XmlArray/XmlAnyElement attributes with explicit Order, then every other member must have an explicit order too."); } if (isOrderExplicit == true) members.Sort ((m1, m2) => (int) m1.XmlAttributes.SortableOrder - (int) m2.XmlAttributes.SortableOrder); foreach (XmlReflectionMember rmember in members) { string ns = map.XmlTypeNamespace; if (rmember.XmlAttributes.XmlIgnore) continue; if (rmember.DeclaringType != null && rmember.DeclaringType != type) { XmlTypeMapping bmap = ImportClassMapping (rmember.DeclaringType, root, defaultNamespace); if (bmap.HasXmlTypeNamespace) ns = bmap.XmlTypeNamespace; } try { XmlTypeMapMember mem = CreateMapMember (type, rmember, ns); mem.CheckOptionalValueType (type); classMap.AddMember (mem); } catch (Exception ex) { throw new InvalidOperationException (string.Format ( CultureInfo.InvariantCulture, "There was an error" + " reflecting field '{0}'.", rmember.MemberName), ex); } } // Import extra classes if (type == typeof (object) && includedTypes != null) { foreach (Type intype in includedTypes) map.DerivedTypes.Add (ImportTypeMapping (intype, defaultNamespace)); } // Register inheritance relations if (type.BaseType != null) { XmlTypeMapping bmap = ImportClassMapping (type.BaseType, root, defaultNamespace); ClassMap cbmap = bmap.ObjectMap as ClassMap; if (type.BaseType != typeof (object)) { map.BaseMap = bmap; if (!cbmap.HasSimpleContent) classMap.SetCanBeSimpleType (false); } // At this point, derived classes of this map must be already registered RegisterDerivedMap (bmap, map); if (cbmap.HasSimpleContent && classMap.ElementMembers != null && classMap.ElementMembers.Count != 1) throw new InvalidOperationException (String.Format (errSimple, map.TypeData.TypeName, map.BaseMap.TypeData.TypeName)); } ImportIncludedTypes (type, defaultNamespace); if (classMap.XmlTextCollector != null && !classMap.HasSimpleContent) { XmlTypeMapMember mem = classMap.XmlTextCollector; if (mem.TypeData.Type != typeof(string) && mem.TypeData.Type != typeof(string[]) && mem.TypeData.Type != typeof(XmlNode[]) && mem.TypeData.Type != typeof(object[])) throw new InvalidOperationException (String.Format (errSimple2, map.TypeData.TypeName, mem.Name, mem.TypeData.TypeName)); } return map; }
// Attributes might be redefined, so there is an existing attribute for the same name, skip it. // FIXME: this is nothing more than just a hack. // Basically it should use // XmlSchemaComplexType.AttributeUses. XmlSchemaObjectCollection CollectAttributeUsesNonOverlap ( XmlSchemaObjectCollection src, ClassMap map) { XmlSchemaObjectCollection atts = new XmlSchemaObjectCollection (); foreach (XmlSchemaAttribute a in src) if (map.GetAttribute (a.QualifiedName.Name, a.QualifiedName.Namespace) == null) atts.Add (a); return atts; }
protected virtual void WriteObjectElementElements(XmlTypeMapping typeMap, object ob) { ClassMap map = (ClassMap)typeMap.ObjectMap; WriteElementMembers(map, ob, false); }
void ImportParticleComplexContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaParticle particle, CodeIdentifiers classIds, bool isMixed) { ImportParticleContent (typeQName, cmap, particle, classIds, false, ref isMixed); if (isMixed) AddTextMember (typeQName, cmap, classIds); }
void WriteMembers(ClassMap map, object ob, bool isValueList) { WriteAttributeMembers(map, ob, isValueList); WriteElementMembers(map, ob, isValueList); }
void ImportParticleContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaParticle particle, CodeIdentifiers classIds, bool multiValue, ref bool isMixed) { if (particle == null) return; if (particle is XmlSchemaGroupRef) particle = GetRefGroupParticle ((XmlSchemaGroupRef)particle); if (particle.MaxOccurs > 1) multiValue = true; if (particle is XmlSchemaSequence) { ImportSequenceContent (typeQName, cmap, ((XmlSchemaSequence)particle).Items, classIds, multiValue, ref isMixed); } else if (particle is XmlSchemaChoice) { if (((XmlSchemaChoice)particle).Items.Count == 1) ImportSequenceContent (typeQName, cmap, ((XmlSchemaChoice)particle).Items, classIds, multiValue, ref isMixed); else ImportChoiceContent (typeQName, cmap, (XmlSchemaChoice)particle, classIds, multiValue); } else if (particle is XmlSchemaAll) { ImportSequenceContent (typeQName, cmap, ((XmlSchemaAll)particle).Items, classIds, multiValue, ref isMixed); } }
XmlTypeMapping ImportClassMapping (TypeData typeData, string defaultNamespace) { Type type = typeData.Type; if (type.IsValueType) throw CreateStructException (type); if (type == typeof (object)) defaultNamespace = XmlSchema.Namespace; ReflectionHelper.CheckSerializableType (type, false); XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, defaultNamespace)); if (map != null) return map; map = CreateTypeMapping (typeData, null, defaultNamespace); helper.RegisterClrType (map, type, map.Namespace); map.MultiReferenceType = true; ClassMap classMap = new ClassMap (); map.ObjectMap = classMap; // Import members ICollection members = GetReflectionMembers (type); foreach (XmlReflectionMember rmember in members) { if (rmember.SoapAttributes.SoapIgnore) continue; classMap.AddMember (CreateMapMember (rmember, defaultNamespace)); } // Import included classes SoapIncludeAttribute[] includes = (SoapIncludeAttribute[])type.GetCustomAttributes (typeof (SoapIncludeAttribute), false); for (int n=0; n<includes.Length; n++) { Type includedType = includes[n].Type; ImportTypeMapping (includedType); } if (type == typeof (object) && includedTypes != null) { foreach (Type intype in includedTypes) map.DerivedTypes.Add (ImportTypeMapping (intype)); } // Register inheritance relations if (type.BaseType != null) { XmlTypeMapping bmap = ImportClassMapping (type.BaseType, defaultNamespace); if (type.BaseType != typeof (object)) map.BaseMap = bmap; // At this point, derived classes of this map must be already registered RegisterDerivedMap (bmap, map); } return map; }
XmlTypeMapping ImportClassMapping (TypeData typeData, XmlRootAttribute root, string defaultNamespace) { Type type = typeData.Type; XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace)); if (map != null) return map; if (!allowPrivateTypes) ReflectionHelper.CheckSerializableType (type, false); map = CreateTypeMapping (typeData, root, null, defaultNamespace); helper.RegisterClrType (map, type, map.XmlTypeNamespace); helper.RegisterSchemaType (map, map.XmlType, map.XmlTypeNamespace); // Import members ClassMap classMap = new ClassMap (); map.ObjectMap = classMap; ICollection members = GetReflectionMembers (type); foreach (XmlReflectionMember rmember in members) { string ns = map.XmlTypeNamespace; if (rmember.XmlAttributes.XmlIgnore) continue; if (rmember.DeclaringType != null && rmember.DeclaringType != type) { XmlTypeMapping bmap = ImportClassMapping (rmember.DeclaringType, root, defaultNamespace); ns = bmap.XmlTypeNamespace; } try { XmlTypeMapMember mem = CreateMapMember (type, rmember, ns); mem.CheckOptionalValueType (type); classMap.AddMember (mem); } catch (Exception ex) { throw new InvalidOperationException (string.Format ( CultureInfo.InvariantCulture, "There was an error" + " reflecting field '{0}'.", rmember.MemberName), ex); } } // Import extra classes if (type == typeof (object) && includedTypes != null) { foreach (Type intype in includedTypes) map.DerivedTypes.Add (ImportTypeMapping (intype, defaultNamespace)); } // Register inheritance relations if (type.BaseType != null) { XmlTypeMapping bmap = ImportClassMapping (type.BaseType, root, defaultNamespace); ClassMap cbmap = bmap.ObjectMap as ClassMap; if (type.BaseType != typeof (object)) { map.BaseMap = bmap; if (!cbmap.HasSimpleContent) classMap.SetCanBeSimpleType (false); } // At this point, derived classes of this map must be already registered RegisterDerivedMap (bmap, map); if (cbmap.HasSimpleContent && classMap.ElementMembers != null && classMap.ElementMembers.Count != 1) throw new InvalidOperationException (String.Format (errSimple, map.TypeData.TypeName, map.BaseMap.TypeData.TypeName)); } ImportIncludedTypes (type, defaultNamespace); if (classMap.XmlTextCollector != null && !classMap.HasSimpleContent) { XmlTypeMapMember mem = classMap.XmlTextCollector; if (mem.TypeData.Type != typeof(string) && mem.TypeData.Type != typeof(string[]) && #if !MOONLIGHT mem.TypeData.Type != typeof(XmlNode[]) && #endif mem.TypeData.Type != typeof(object[])) throw new InvalidOperationException (String.Format (errSimple2, map.TypeData.TypeName, mem.Name, mem.TypeData.TypeName)); } return map; }
void WriteMembers (ClassMap map, object ob, bool isValueList) { WriteAttributeMembers (map, ob, isValueList); WriteElementMembers (map, ob, isValueList); }