private void WriteAttributeMembers(ClassMap map, object ob, bool isValueList) { XmlTypeMapMember defaultAnyAttributeMember = map.DefaultAnyAttributeMember; if (defaultAnyAttributeMember != null && this.MemberHasValue(defaultAnyAttributeMember, ob, isValueList)) { ICollection collection = (ICollection)this.GetMemberValue(defaultAnyAttributeMember, ob, isValueList); if (collection != null) { foreach (object obj in collection) { XmlAttribute xmlAttribute = (XmlAttribute)obj; if (xmlAttribute.NamespaceURI != "http://www.w3.org/2000/xmlns/") { base.WriteXmlAttribute(xmlAttribute, ob); } } } } ICollection attributeMembers = map.AttributeMembers; if (attributeMembers != null) { foreach (object obj2 in attributeMembers) { XmlTypeMapMemberAttribute xmlTypeMapMemberAttribute = (XmlTypeMapMemberAttribute)obj2; if (this.MemberHasValue(xmlTypeMapMemberAttribute, ob, isValueList)) { base.WriteAttribute(xmlTypeMapMemberAttribute.AttributeName, xmlTypeMapMemberAttribute.Namespace, this.GetStringValue(xmlTypeMapMemberAttribute.MappedType, xmlTypeMapMemberAttribute.TypeData, this.GetMemberValue(xmlTypeMapMemberAttribute, ob, isValueList))); } } } }
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); } } } #if !MOONLIGHT else if (anyAttrMember != null) { XmlAttribute attr = (XmlAttribute)Document.ReadNode(Reader); ParseWsdlArrayType(attr); AddListValue(anyAttrMember.TypeData, ref anyAttributeArray, anyAttributeIndex++, attr, true); } else { ProcessUnknownAttribute(ob); } #endif } #if !MOONLIGHT if (anyAttrMember != null) { anyAttributeArray = ShrinkArray((Array)anyAttributeArray, anyAttributeIndex, anyAttrMember.TypeData.Type.GetElementType(), true); SetMemberValue(anyAttrMember, ob, anyAttributeArray, isValueList); } #endif Reader.MoveToElement(); }
void AddAttributeFieldMember(CodeTypeDeclaration codeClass, XmlTypeMapMemberAttribute attinfo, string defaultNamespace) { CodeTypeMember codeField = CreateFieldMember(codeClass, attinfo); CodeAttributeDeclarationCollection attributes = codeField.CustomAttributes; if (attributes == null) { attributes = new CodeAttributeDeclarationCollection(); } GenerateAttributeMember(attributes, attinfo, defaultNamespace, false); if (attributes.Count > 0) { codeField.CustomAttributes = attributes; } if (attinfo.MappedType != null) { ExportMapCode(attinfo.MappedType, false); RemoveInclude(attinfo.MappedType); } if (attinfo.TypeData.IsValueType && attinfo.IsOptionalValueType) { codeField = CreateFieldMember(codeClass, typeof(bool), identifiers.MakeUnique(attinfo.Name + "Specified")); codeField.Attributes = MemberAttributes.Public; GenerateSpecifierMember(codeField); } }
XmlSchemaAttribute GetSchemaAttribute(XmlSchema currentSchema, XmlTypeMapMemberAttribute attinfo, bool isTypeMember) { XmlSchemaAttribute sat = new XmlSchemaAttribute(); if (attinfo.DefaultValue != System.DBNull.Value) { sat.DefaultValue = XmlCustomFormatter.ToXmlString(attinfo.TypeData, attinfo.DefaultValue); } ImportNamespace(currentSchema, attinfo.Namespace); XmlSchema memberSchema; if (attinfo.Namespace.Length == 0 && attinfo.Form != XmlSchemaForm.Qualified) { memberSchema = currentSchema; } else { memberSchema = GetSchema(attinfo.Namespace); } if (currentSchema == memberSchema || encodedFormat) { sat.Name = attinfo.AttributeName; if (isTypeMember) { sat.Form = attinfo.Form; } if (attinfo.TypeData.SchemaType == SchemaTypes.Enum) { ImportNamespace(currentSchema, attinfo.DataTypeNamespace); ExportEnumSchema(attinfo.MappedType); sat.SchemaTypeName = new XmlQualifiedName(attinfo.TypeData.XmlType, attinfo.DataTypeNamespace);; } else if (attinfo.TypeData.SchemaType == SchemaTypes.Array && TypeTranslator.IsPrimitive(attinfo.TypeData.ListItemType)) { sat.SchemaType = GetSchemaSimpleListType(attinfo.TypeData); } else { sat.SchemaTypeName = new XmlQualifiedName(attinfo.TypeData.XmlType, attinfo.DataTypeNamespace); }; } else { sat.RefName = new XmlQualifiedName(attinfo.AttributeName, attinfo.Namespace); foreach (XmlSchemaObject ob in memberSchema.Items) { if (ob is XmlSchemaAttribute && ((XmlSchemaAttribute)ob).Name == attinfo.AttributeName) { return(sat); } } memberSchema.Items.Add(GetSchemaAttribute(memberSchema, attinfo, false)); } return(sat); }
private XmlSchemaAttribute GetSchemaAttribute(XmlSchema currentSchema, XmlTypeMapMemberAttribute attinfo, bool isTypeMember) { XmlSchemaAttribute xmlSchemaAttribute = new XmlSchemaAttribute(); if (attinfo.DefaultValue != DBNull.Value) { xmlSchemaAttribute.DefaultValue = this.ExportDefaultValue(attinfo.TypeData, attinfo.MappedType, attinfo.DefaultValue); } else if (!attinfo.IsOptionalValueType && attinfo.TypeData.IsValueType) { xmlSchemaAttribute.Use = XmlSchemaUse.Required; } this.ImportNamespace(currentSchema, attinfo.Namespace); XmlSchema xmlSchema; if (attinfo.Namespace.Length == 0 && attinfo.Form != XmlSchemaForm.Qualified) { xmlSchema = currentSchema; } else { xmlSchema = this.GetSchema(attinfo.Namespace); } if (currentSchema != xmlSchema && !this.encodedFormat) { xmlSchemaAttribute.RefName = new XmlQualifiedName(attinfo.AttributeName, attinfo.Namespace); foreach (XmlSchemaObject xmlSchemaObject in xmlSchema.Items) { if (xmlSchemaObject is XmlSchemaAttribute && ((XmlSchemaAttribute)xmlSchemaObject).Name == attinfo.AttributeName) { return(xmlSchemaAttribute); } } xmlSchema.Items.Add(this.GetSchemaAttribute(xmlSchema, attinfo, false)); return(xmlSchemaAttribute); } xmlSchemaAttribute.Name = attinfo.AttributeName; if (isTypeMember) { xmlSchemaAttribute.Form = attinfo.Form; } if (attinfo.TypeData.SchemaType == SchemaTypes.Enum) { this.ImportNamespace(currentSchema, attinfo.DataTypeNamespace); this.ExportEnumSchema(attinfo.MappedType); xmlSchemaAttribute.SchemaTypeName = new XmlQualifiedName(attinfo.TypeData.XmlType, attinfo.DataTypeNamespace); } else if (attinfo.TypeData.SchemaType == SchemaTypes.Array && TypeTranslator.IsPrimitive(attinfo.TypeData.ListItemType)) { xmlSchemaAttribute.SchemaType = this.GetSchemaSimpleListType(attinfo.TypeData); } else { xmlSchemaAttribute.SchemaTypeName = new XmlQualifiedName(attinfo.TypeData.XmlType, attinfo.DataTypeNamespace); } return(xmlSchemaAttribute); }
private void ReadAttributeMembers(ClassMap map, object ob, bool isValueList) { XmlTypeMapMember defaultAnyAttributeMember = map.DefaultAnyAttributeMember; int length = 0; object obj = null; while (base.Reader.MoveToNextAttribute()) { XmlTypeMapMemberAttribute attribute = map.GetAttribute(base.Reader.LocalName, base.Reader.NamespaceURI); if (attribute != null) { this.SetMemberValue(attribute, ob, this.GetValueFromXmlString(base.Reader.Value, attribute.TypeData, attribute.MappedType), isValueList); } else if (base.IsXmlnsAttribute(base.Reader.Name)) { if (map.NamespaceDeclarations != null) { XmlSerializerNamespaces xmlSerializerNamespaces = this.GetMemberValue(map.NamespaceDeclarations, ob, isValueList) as XmlSerializerNamespaces; if (xmlSerializerNamespaces == null) { xmlSerializerNamespaces = new XmlSerializerNamespaces(); this.SetMemberValue(map.NamespaceDeclarations, ob, xmlSerializerNamespaces, isValueList); } if (base.Reader.Prefix == "xmlns") { xmlSerializerNamespaces.Add(base.Reader.LocalName, base.Reader.Value); } else { xmlSerializerNamespaces.Add(string.Empty, base.Reader.Value); } } } else if (defaultAnyAttributeMember != null) { XmlAttribute xmlAttribute = (XmlAttribute)base.Document.ReadNode(base.Reader); base.ParseWsdlArrayType(xmlAttribute); this.AddListValue(defaultAnyAttributeMember.TypeData, ref obj, length++, xmlAttribute, true); } else { this.ProcessUnknownAttribute(ob); } } if (defaultAnyAttributeMember != null) { obj = base.ShrinkArray((Array)obj, length, defaultAnyAttributeMember.TypeData.Type.GetElementType(), true); this.SetMemberValue(defaultAnyAttributeMember, ob, obj, isValueList); } base.Reader.MoveToElement(); }
protected override void GenerateAttributeMember(CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberAttribute attinfo, string defaultNamespace, bool forceUseMemberName) { CodeAttributeDeclaration att = new CodeAttributeDeclaration("System.Xml.Serialization.XmlAttribute"); if (forceUseMemberName || attinfo.Name != attinfo.AttributeName) { att.Arguments.Add(GetArg(attinfo.AttributeName)); } if (attinfo.Namespace != defaultNamespace) { att.Arguments.Add(GetArg("Namespace", attinfo.Namespace)); } if (attinfo.Form != XmlSchemaForm.None) { att.Arguments.Add(GetEnumArg("Form", "System.Xml.Schema.XmlSchemaForm", attinfo.Form.ToString())); } if (!TypeTranslator.IsDefaultPrimitiveTpeData(attinfo.TypeData)) { att.Arguments.Add(GetArg("DataType", attinfo.TypeData.XmlType)); } attributes.Add(att); if (attinfo.Ignore) { attributes.Add(new CodeAttributeDeclaration("System.Xml.Serialization.XmlIgnoreAttribute")); } }
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); }
protected override void GenerateAttributeMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberAttribute attinfo, string defaultNamespace, bool forceUseMemberName) { CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.SoapAttribute"); if (attinfo.Name != attinfo.AttributeName) att.Arguments.Add (GetArg (attinfo.AttributeName)); if (attinfo.Namespace != defaultNamespace) att.Arguments.Add (GetArg ("Namespace", attinfo.Namespace)); if (!TypeTranslator.IsDefaultPrimitiveTpeData(attinfo.TypeData)) att.Arguments.Add (GetArg ("DataType",attinfo.TypeData.XmlType)); attributes.Add (att); }
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); } } }
protected virtual void GenerateAttributeMember(CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberAttribute attinfo, string defaultNamespace, bool forceUseMemberName) { }
protected override void GenerateAttributeMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberAttribute attinfo, string defaultNamespace, bool forceUseMemberName) { CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlAttributeAttribute"); if (forceUseMemberName || attinfo.Name != attinfo.AttributeName) att.Arguments.Add (GetArg (attinfo.AttributeName)); if (attinfo.Namespace != defaultNamespace) att.Arguments.Add (GetArg ("Namespace", attinfo.Namespace)); if (attinfo.Form == XmlSchemaForm.Qualified) att.Arguments.Add (GetEnumArg ("Form","System.Xml.Schema.XmlSchemaForm",attinfo.Form.ToString())); if (!TypeTranslator.IsDefaultPrimitiveTpeData(attinfo.TypeData)) att.Arguments.Add (GetArg ("DataType",attinfo.TypeData.XmlType)); attributes.Add (att); if (attinfo.Ignore) attributes.Add (new CodeAttributeDeclaration ("System.Xml.Serialization.XmlIgnoreAttribute")); }
XmlSchemaAttribute GetSchemaAttribute (XmlSchema currentSchema, XmlTypeMapMemberAttribute attinfo, bool isTypeMember) { XmlSchemaAttribute sat = new XmlSchemaAttribute (); if (attinfo.DefaultValue != System.DBNull.Value) { sat.DefaultValue = ExportDefaultValue (attinfo.TypeData, attinfo.MappedType, attinfo.DefaultValue); } else { if (!attinfo.IsOptionalValueType && attinfo.TypeData.IsValueType) sat.Use = XmlSchemaUse.Required; } ImportNamespace (currentSchema, attinfo.Namespace); XmlSchema memberSchema; if (attinfo.Namespace.Length == 0 && attinfo.Form != XmlSchemaForm.Qualified) memberSchema = currentSchema; else memberSchema = GetSchema (attinfo.Namespace); if (currentSchema == memberSchema || encodedFormat) { sat.Name = attinfo.AttributeName; if (isTypeMember) sat.Form = attinfo.Form; if (attinfo.TypeData.SchemaType == SchemaTypes.Enum) { ImportNamespace (currentSchema, attinfo.DataTypeNamespace); ExportEnumSchema (attinfo.MappedType); sat.SchemaTypeName = new XmlQualifiedName (attinfo.TypeData.XmlType, attinfo.DataTypeNamespace); } else if (attinfo.TypeData.SchemaType == SchemaTypes.Array && TypeTranslator.IsPrimitive (attinfo.TypeData.ListItemType)) { sat.SchemaType = GetSchemaSimpleListType (attinfo.TypeData); } else sat.SchemaTypeName = new XmlQualifiedName (attinfo.TypeData.XmlType, attinfo.DataTypeNamespace);; } else { sat.RefName = new XmlQualifiedName (attinfo.AttributeName, attinfo.Namespace); foreach (XmlSchemaObject ob in memberSchema.Items) if (ob is XmlSchemaAttribute && ((XmlSchemaAttribute)ob).Name == attinfo.AttributeName) return sat; memberSchema.Items.Add (GetSchemaAttribute (memberSchema, attinfo, false)); } return sat; }
private XmlTypeMapMember CreateMapMember(XmlReflectionMember rmember, string defaultNamespace) { XmlTypeMapMember mapMember; SoapAttributes atts = rmember.SoapAttributes; TypeData typeData = TypeTranslator.GetTypeData(rmember.MemberType); if (atts.SoapAttribute != null) { // An attribute if (typeData.SchemaType != SchemaTypes.Enum && typeData.SchemaType != SchemaTypes.Primitive) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Cannot serialize member '{0}' of type {1}. " + "SoapAttribute cannot be used to encode complex types.", rmember.MemberName, typeData.FullTypeName)); } if (atts.SoapElement != null) { throw new Exception("SoapAttributeAttribute and SoapElementAttribute cannot be applied to the same member"); } XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute(); if (atts.SoapAttribute.AttributeName.Length == 0) { mapAttribute.AttributeName = XmlConvert.EncodeLocalName(rmember.MemberName); } else { mapAttribute.AttributeName = XmlConvert.EncodeLocalName(atts.SoapAttribute.AttributeName); } mapAttribute.Namespace = (atts.SoapAttribute.Namespace != null) ? atts.SoapAttribute.Namespace : ""; if (typeData.IsComplexType) { mapAttribute.MappedType = ImportTypeMapping(typeData.Type, defaultNamespace); } typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.SoapAttribute.DataType); mapMember = mapAttribute; mapMember.DefaultValue = GetDefaultValue(typeData, atts.SoapDefaultValue); } else { if (typeData.SchemaType == SchemaTypes.Array) { mapMember = new XmlTypeMapMemberList(); } else { mapMember = new XmlTypeMapMemberElement(); } if (atts.SoapElement != null && atts.SoapElement.DataType.Length != 0) { typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.SoapElement.DataType); } // Creates an ElementInfo that identifies the element XmlTypeMapElementInfoList infoList = new XmlTypeMapElementInfoList(); XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo(mapMember, typeData); elem.ElementName = XmlConvert.EncodeLocalName((atts.SoapElement != null && atts.SoapElement.ElementName.Length != 0) ? atts.SoapElement.ElementName : rmember.MemberName); elem.Namespace = string.Empty; elem.IsNullable = (atts.SoapElement != null) ? atts.SoapElement.IsNullable : false; if (typeData.IsComplexType) { elem.MappedType = ImportTypeMapping(typeData.Type, defaultNamespace); } infoList.Add(elem); ((XmlTypeMapMemberElement)mapMember).ElementInfo = infoList; } mapMember.TypeData = typeData; mapMember.Name = rmember.MemberName; mapMember.IsReturnValue = rmember.IsReturnValue; return(mapMember); }
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 XmlTypeMapMember CreateMapMember (XmlReflectionMember rmember, string defaultNamespace) { XmlTypeMapMember mapMember; XmlAttributes atts = rmember.XmlAttributes; TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType); if (atts.XmlAnyAttribute != null) { if ( (rmember.MemberType.FullName == "System.Xml.XmlAttribute[]") || (rmember.MemberType.FullName == "System.Xml.XmlNode[]") ) { mapMember = new XmlTypeMapMemberAnyAttribute(); } else throw new InvalidOperationException ("XmlAnyAttributeAttribute can only be applied to members of type XmlAttribute[] or XmlNode[]"); } else if (atts.XmlAnyElements != null && atts.XmlAnyElements.Count > 0) { if ( (rmember.MemberType.FullName == "System.Xml.XmlElement[]") || (rmember.MemberType.FullName == "System.Xml.XmlNode[]") || (rmember.MemberType.FullName == "System.Xml.XmlElement")) { XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement(); member.ElementInfo = ImportAnyElementInfo (defaultNamespace, rmember, member, atts); mapMember = member; } else throw new InvalidOperationException ("XmlAnyElementAttribute can only be applied to members of type XmlElement, XmlElement[] or XmlNode[]"); } else if (atts.Xmlns) { XmlTypeMapMemberNamespaces mapNamespaces = new XmlTypeMapMemberNamespaces (); mapMember = mapNamespaces; } else if (atts.XmlAttribute != null) { // An attribute if (atts.XmlElements != null && atts.XmlElements.Count > 0) throw new Exception ("XmlAttributeAttribute and XmlElementAttribute cannot be applied to the same member"); XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute (); if (atts.XmlAttribute.AttributeName == null) mapAttribute.AttributeName = rmember.MemberName; else mapAttribute.AttributeName = atts.XmlAttribute.AttributeName; if (typeData.IsComplexType) mapAttribute.MappedType = ImportTypeMapping (typeData.Type, null, mapAttribute.Namespace); if (atts.XmlAttribute.Namespace != null && atts.XmlAttribute.Namespace != defaultNamespace) { if (atts.XmlAttribute.Form == XmlSchemaForm.Unqualified) throw new InvalidOperationException ("The Form property may not be 'Unqualified' when an explicit Namespace property is present"); mapAttribute.Form = XmlSchemaForm.Qualified; mapAttribute.Namespace = atts.XmlAttribute.Namespace; } else { mapAttribute.Form = atts.XmlAttribute.Form; if (atts.XmlAttribute.Form == XmlSchemaForm.Qualified) mapAttribute.Namespace = defaultNamespace; else mapAttribute.Namespace = ""; } typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.XmlAttribute.DataType); mapMember = mapAttribute; } else if (typeData.SchemaType == SchemaTypes.Array) { // If the member has a single XmlElementAttribute and the type is the type of the member, // then it is not a flat list if (atts.XmlElements.Count > 1 || (atts.XmlElements.Count == 1 && atts.XmlElements[0].Type != typeData.Type) || (atts.XmlText != null)) { // A flat list // TODO: check that it does not have XmlArrayAttribute XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList (); member.ListMap = new ListMap (); member.ListMap.ItemInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, typeData.ListItemType, member, atts); member.ElementInfo = member.ListMap.ItemInfo; mapMember = member; } else { // A list XmlTypeMapMemberList member = new XmlTypeMapMemberList (); // Creates an ElementInfo that identifies the array instance. member.ElementInfo = new XmlTypeMapElementInfoList(); XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, typeData); elem.ElementName = (atts.XmlArray != null && atts.XmlArray.ElementName != null) ? atts.XmlArray.ElementName : rmember.MemberName; elem.Namespace = (atts.XmlArray != null && atts.XmlArray.Namespace != null) ? atts.XmlArray.Namespace : defaultNamespace; elem.MappedType = ImportListMapping (rmember.MemberType, null, elem.Namespace, atts, 0); elem.IsNullable = (atts.XmlArray != null) ? atts.XmlArray.IsNullable : false; elem.Form = (atts.XmlArray != null) ? atts.XmlArray.Form : XmlSchemaForm.Qualified; member.ElementInfo.Add (elem); mapMember = member; } } else { // An element XmlTypeMapMemberElement member = new XmlTypeMapMemberElement (); member.ElementInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, rmember.MemberType, member, atts); mapMember = member; } mapMember.DefaultValue = atts.XmlDefaultValue; mapMember.TypeData = typeData; mapMember.Name = rmember.MemberName; mapMember.IsReturnValue = rmember.IsReturnValue; return mapMember; }
private XmlTypeMapMember CreateMapMember(XmlReflectionMember rmember, string defaultNamespace) { SoapAttributes soapAttributes = rmember.SoapAttributes; TypeData typeData = TypeTranslator.GetTypeData(rmember.MemberType); XmlTypeMapMember xmlTypeMapMember; if (soapAttributes.SoapAttribute != null) { if (typeData.SchemaType != SchemaTypes.Enum && typeData.SchemaType != SchemaTypes.Primitive) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Cannot serialize member '{0}' of type {1}. SoapAttribute cannot be used to encode complex types.", new object[] { rmember.MemberName, typeData.FullTypeName })); } if (soapAttributes.SoapElement != null) { throw new Exception("SoapAttributeAttribute and SoapElementAttribute cannot be applied to the same member"); } XmlTypeMapMemberAttribute xmlTypeMapMemberAttribute = new XmlTypeMapMemberAttribute(); if (soapAttributes.SoapAttribute.AttributeName.Length == 0) { xmlTypeMapMemberAttribute.AttributeName = XmlConvert.EncodeLocalName(rmember.MemberName); } else { xmlTypeMapMemberAttribute.AttributeName = XmlConvert.EncodeLocalName(soapAttributes.SoapAttribute.AttributeName); } xmlTypeMapMemberAttribute.Namespace = ((soapAttributes.SoapAttribute.Namespace == null) ? string.Empty : soapAttributes.SoapAttribute.Namespace); if (typeData.IsComplexType) { xmlTypeMapMemberAttribute.MappedType = this.ImportTypeMapping(typeData.Type, defaultNamespace); } typeData = TypeTranslator.GetTypeData(rmember.MemberType, soapAttributes.SoapAttribute.DataType); xmlTypeMapMember = xmlTypeMapMemberAttribute; xmlTypeMapMember.DefaultValue = this.GetDefaultValue(typeData, soapAttributes.SoapDefaultValue); } else { if (typeData.SchemaType == SchemaTypes.Array) { xmlTypeMapMember = new XmlTypeMapMemberList(); } else { xmlTypeMapMember = new XmlTypeMapMemberElement(); } if (soapAttributes.SoapElement != null && soapAttributes.SoapElement.DataType.Length != 0) { typeData = TypeTranslator.GetTypeData(rmember.MemberType, soapAttributes.SoapElement.DataType); } XmlTypeMapElementInfoList xmlTypeMapElementInfoList = new XmlTypeMapElementInfoList(); XmlTypeMapElementInfo xmlTypeMapElementInfo = new XmlTypeMapElementInfo(xmlTypeMapMember, typeData); xmlTypeMapElementInfo.ElementName = XmlConvert.EncodeLocalName((soapAttributes.SoapElement == null || soapAttributes.SoapElement.ElementName.Length == 0) ? rmember.MemberName : soapAttributes.SoapElement.ElementName); xmlTypeMapElementInfo.Namespace = string.Empty; xmlTypeMapElementInfo.IsNullable = (soapAttributes.SoapElement != null && soapAttributes.SoapElement.IsNullable); if (typeData.IsComplexType) { xmlTypeMapElementInfo.MappedType = this.ImportTypeMapping(typeData.Type, defaultNamespace); } xmlTypeMapElementInfoList.Add(xmlTypeMapElementInfo); ((XmlTypeMapMemberElement)xmlTypeMapMember).ElementInfo = xmlTypeMapElementInfoList; } xmlTypeMapMember.TypeData = typeData; xmlTypeMapMember.Name = rmember.MemberName; xmlTypeMapMember.IsReturnValue = rmember.IsReturnValue; return(xmlTypeMapMember); }
private XmlTypeMapMember CreateMapMember(XmlReflectionMember rmember, string defaultNamespace) { XmlTypeMapMember mapMember; XmlAttributes atts = rmember.XmlAttributes; TypeData typeData = TypeTranslator.GetTypeData(rmember.MemberType); if (atts.XmlAnyAttribute != null) { if ((rmember.MemberType.FullName == "System.Xml.XmlAttribute[]") || (rmember.MemberType.FullName == "System.Xml.XmlNode[]")) { mapMember = new XmlTypeMapMemberAnyAttribute(); } else { throw new InvalidOperationException("XmlAnyAttributeAttribute can only be applied to members of type XmlAttribute[] or XmlNode[]"); } } else if (atts.XmlAnyElements != null && atts.XmlAnyElements.Count > 0) { if ((rmember.MemberType.FullName == "System.Xml.XmlElement[]") || (rmember.MemberType.FullName == "System.Xml.XmlNode[]") || (rmember.MemberType.FullName == "System.Xml.XmlElement")) { XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement(); member.ElementInfo = ImportAnyElementInfo(defaultNamespace, rmember, member, atts); mapMember = member; } else { throw new InvalidOperationException("XmlAnyElementAttribute can only be applied to members of type XmlElement, XmlElement[] or XmlNode[]"); } } else if (atts.Xmlns) { XmlTypeMapMemberNamespaces mapNamespaces = new XmlTypeMapMemberNamespaces(); mapMember = mapNamespaces; } else if (atts.XmlAttribute != null) { // An attribute if (atts.XmlElements != null && atts.XmlElements.Count > 0) { throw new Exception("XmlAttributeAttribute and XmlElementAttribute cannot be applied to the same member"); } XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute(); if (atts.XmlAttribute.AttributeName == null) { mapAttribute.AttributeName = rmember.MemberName; } else { mapAttribute.AttributeName = atts.XmlAttribute.AttributeName; } if (typeData.IsComplexType) { mapAttribute.MappedType = ImportTypeMapping(typeData.Type, null, mapAttribute.Namespace); } if (atts.XmlAttribute.Namespace != null && atts.XmlAttribute.Namespace != defaultNamespace) { if (atts.XmlAttribute.Form == XmlSchemaForm.Unqualified) { throw new InvalidOperationException("The Form property may not be 'Unqualified' when an explicit Namespace property is present"); } mapAttribute.Form = XmlSchemaForm.Qualified; mapAttribute.Namespace = atts.XmlAttribute.Namespace; } else { mapAttribute.Form = atts.XmlAttribute.Form; if (atts.XmlAttribute.Form == XmlSchemaForm.Qualified) { mapAttribute.Namespace = defaultNamespace; } else { mapAttribute.Namespace = ""; } } typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.XmlAttribute.DataType); mapMember = mapAttribute; } else if (typeData.SchemaType == SchemaTypes.Array) { // If the member has a single XmlElementAttribute and the type is the type of the member, // then it is not a flat list if (atts.XmlElements.Count > 1 || (atts.XmlElements.Count == 1 && atts.XmlElements[0].Type != typeData.Type) || (atts.XmlText != null)) { // A flat list // TODO: check that it does not have XmlArrayAttribute XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList(); member.ListMap = new ListMap(); member.ListMap.ItemInfo = ImportElementInfo(rmember.MemberName, defaultNamespace, typeData.ListItemType, member, atts); member.ElementInfo = member.ListMap.ItemInfo; mapMember = member; } else { // A list XmlTypeMapMemberList member = new XmlTypeMapMemberList(); // Creates an ElementInfo that identifies the array instance. member.ElementInfo = new XmlTypeMapElementInfoList(); XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo(member, typeData); elem.ElementName = (atts.XmlArray != null && atts.XmlArray.ElementName != null) ? atts.XmlArray.ElementName : rmember.MemberName; elem.Namespace = (atts.XmlArray != null && atts.XmlArray.Namespace != null) ? atts.XmlArray.Namespace : defaultNamespace; elem.MappedType = ImportListMapping(rmember.MemberType, null, elem.Namespace, atts, 0); elem.IsNullable = (atts.XmlArray != null) ? atts.XmlArray.IsNullable : false; elem.Form = (atts.XmlArray != null) ? atts.XmlArray.Form : XmlSchemaForm.Qualified; member.ElementInfo.Add(elem); mapMember = member; } } else { // An element XmlTypeMapMemberElement member = new XmlTypeMapMemberElement(); member.ElementInfo = ImportElementInfo(rmember.MemberName, defaultNamespace, rmember.MemberType, member, atts); mapMember = member; } mapMember.DefaultValue = atts.XmlDefaultValue; mapMember.TypeData = typeData; mapMember.Name = rmember.MemberName; mapMember.IsReturnValue = rmember.IsReturnValue; return(mapMember); }
void AddAttributeFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMemberAttribute attinfo, string defaultNamespace) { CodeMemberField codeField = CreateFieldMember (attinfo); codeClass.Members.Add (codeField); CodeAttributeDeclarationCollection attributes = codeField.CustomAttributes; if (attributes == null) attributes = new CodeAttributeDeclarationCollection (); GenerateAttributeMember (attributes, attinfo, defaultNamespace, false); if (attributes.Count > 0) codeField.CustomAttributes = attributes; if (attinfo.MappedType != null) { ExportMapCode (attinfo.MappedType); RemoveInclude (attinfo.MappedType); } if (attinfo.TypeData.IsValueType && attinfo.IsOptionalValueType) { codeField = new CodeMemberField (typeof(bool), attinfo.Name + "Specified"); codeField.Attributes = MemberAttributes.Public; codeClass.Members.Add (codeField); GenerateSpecifierMember (codeField); } }
private XmlTypeMapMember CreateMapMember (XmlReflectionMember rmember, string defaultNamespace) { XmlTypeMapMember mapMember; SoapAttributes atts = rmember.SoapAttributes; TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType); if (atts.SoapAttribute != null) { // An attribute if (typeData.SchemaType != SchemaTypes.Enum && typeData.SchemaType != SchemaTypes.Primitive) { throw new InvalidOperationException (string.Format (CultureInfo.InvariantCulture, "Cannot serialize member '{0}' of type {1}. " + "SoapAttribute cannot be used to encode complex types.", rmember.MemberName, typeData.FullTypeName)); } if (atts.SoapElement != null) throw new Exception ("SoapAttributeAttribute and SoapElementAttribute cannot be applied to the same member"); XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute (); if (atts.SoapAttribute.AttributeName.Length == 0) mapAttribute.AttributeName = XmlConvert.EncodeLocalName (rmember.MemberName); else mapAttribute.AttributeName = XmlConvert.EncodeLocalName (atts.SoapAttribute.AttributeName); mapAttribute.Namespace = (atts.SoapAttribute.Namespace != null) ? atts.SoapAttribute.Namespace : ""; if (typeData.IsComplexType) mapAttribute.MappedType = ImportTypeMapping (typeData.Type, defaultNamespace); typeData = TypeTranslator.GetTypeData (rmember.MemberType, atts.SoapAttribute.DataType); mapMember = mapAttribute; mapMember.DefaultValue = GetDefaultValue (typeData, atts.SoapDefaultValue); } else { if (typeData.SchemaType == SchemaTypes.Array) mapMember = new XmlTypeMapMemberList (); else mapMember = new XmlTypeMapMemberElement (); if (atts.SoapElement != null && atts.SoapElement.DataType.Length != 0) typeData = TypeTranslator.GetTypeData (rmember.MemberType, atts.SoapElement.DataType); // Creates an ElementInfo that identifies the element XmlTypeMapElementInfoList infoList = new XmlTypeMapElementInfoList(); XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (mapMember, typeData); elem.ElementName = XmlConvert.EncodeLocalName ((atts.SoapElement != null && atts.SoapElement.ElementName.Length != 0) ? atts.SoapElement.ElementName : rmember.MemberName); elem.Namespace = string.Empty; elem.IsNullable = (atts.SoapElement != null) ? atts.SoapElement.IsNullable : false; if (typeData.IsComplexType) elem.MappedType = ImportTypeMapping (typeData.Type, defaultNamespace); infoList.Add (elem); ((XmlTypeMapMemberElement)mapMember).ElementInfo = infoList; } mapMember.TypeData = typeData; mapMember.Name = rmember.MemberName; mapMember.IsReturnValue = rmember.IsReturnValue; return mapMember; }
public void AddAttributeMemberAttributes (XmlTypeMapMemberAttribute attinfo, string defaultNamespace, CodeAttributeDeclarationCollection attributes, bool forceUseMemberName) { GenerateAttributeMember (attributes, attinfo, defaultNamespace, forceUseMemberName); }
public void AddAttributeMemberAttributes(XmlTypeMapMemberAttribute attinfo, string defaultNamespace, CodeAttributeDeclarationCollection attributes, bool forceUseMemberName) { GenerateAttributeMember(attributes, attinfo, defaultNamespace, forceUseMemberName); }
protected virtual void GenerateAttributeMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberAttribute attinfo, string defaultNamespace, bool forceUseMemberName) { }
protected override void GenerateAttributeMember(CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberAttribute attinfo, string defaultNamespace, bool forceUseMemberName) { CodeAttributeDeclaration att = new CodeAttributeDeclaration("System.Xml.Serialization.SoapAttribute"); if (attinfo.Name != attinfo.AttributeName) { att.Arguments.Add(GetArg(attinfo.AttributeName)); } if (attinfo.Namespace != defaultNamespace) { att.Arguments.Add(GetArg("Namespace", attinfo.Namespace)); } if (!TypeTranslator.IsDefaultPrimitiveTpeData(attinfo.TypeData)) { att.Arguments.Add(GetArg("DataType", attinfo.TypeData.XmlType)); } attributes.Add(att); }
public void AddMember(XmlTypeMapMember member) { member.GlobalIndex = this._allMembers.Count; this._allMembers.Add(member); if (!(member.DefaultValue is DBNull) && member.DefaultValue != null) { if (this._membersWithDefault == null) { this._membersWithDefault = new ArrayList(); } this._membersWithDefault.Add(member); } if (member.IsReturnValue) { this._returnMember = member; } if (!(member is XmlTypeMapMemberAttribute)) { if (member is XmlTypeMapMemberFlatList) { this.RegisterFlatList((XmlTypeMapMemberFlatList)member); } else if (member is XmlTypeMapMemberAnyElement) { XmlTypeMapMemberAnyElement xmlTypeMapMemberAnyElement = (XmlTypeMapMemberAnyElement)member; if (xmlTypeMapMemberAnyElement.IsDefaultAny) { this._defaultAnyElement = xmlTypeMapMemberAnyElement; } if (xmlTypeMapMemberAnyElement.TypeData.IsListType) { this.RegisterFlatList(xmlTypeMapMemberAnyElement); } } else { if (member is XmlTypeMapMemberAnyAttribute) { this._defaultAnyAttribute = (XmlTypeMapMemberAnyAttribute)member; return; } if (member is XmlTypeMapMemberNamespaces) { this._namespaceDeclarations = (XmlTypeMapMemberNamespaces)member; return; } } if (member is XmlTypeMapMemberElement && ((XmlTypeMapMemberElement)member).IsXmlTextCollector) { if (this._xmlTextCollector != null) { throw new InvalidOperationException("XmlTextAttribute can only be applied once in a class"); } this._xmlTextCollector = member; } if (this._elementMembers == null) { this._elementMembers = new ArrayList(); this._elements = new Hashtable(); } member.Index = this._elementMembers.Count; this._elementMembers.Add(member); ICollection elementInfo = ((XmlTypeMapMemberElement)member).ElementInfo; foreach (object obj in elementInfo) { XmlTypeMapElementInfo xmlTypeMapElementInfo = (XmlTypeMapElementInfo)obj; string key = this.BuildKey(xmlTypeMapElementInfo.ElementName, xmlTypeMapElementInfo.Namespace); if (this._elements.ContainsKey(key)) { throw new InvalidOperationException(string.Concat(new string[] { "The XML element named '", xmlTypeMapElementInfo.ElementName, "' from namespace '", xmlTypeMapElementInfo.Namespace, "' is already present in the current scope. Use XML attributes to specify another XML name or namespace for the element." })); } this._elements.Add(key, xmlTypeMapElementInfo); } if (member.TypeData.IsListType && member.TypeData.Type != null && !member.TypeData.Type.IsArray) { if (this._listMembers == null) { this._listMembers = new ArrayList(); } this._listMembers.Add(member); } return; } XmlTypeMapMemberAttribute xmlTypeMapMemberAttribute = (XmlTypeMapMemberAttribute)member; if (this._attributeMembers == null) { this._attributeMembers = new Hashtable(); } string key2 = this.BuildKey(xmlTypeMapMemberAttribute.AttributeName, xmlTypeMapMemberAttribute.Namespace); if (this._attributeMembers.ContainsKey(key2)) { throw new InvalidOperationException(string.Concat(new string[] { "The XML attribute named '", xmlTypeMapMemberAttribute.AttributeName, "' from namespace '", xmlTypeMapMemberAttribute.Namespace, "' is already present in the current scope. Use XML attributes to specify another XML name or namespace for the attribute." })); } member.Index = this._attributeMembers.Count; this._attributeMembers.Add(key2, member); }
private XmlTypeMapMember CreateMapMember (Type declaringType, XmlReflectionMember rmember, string defaultNamespace) { XmlTypeMapMember mapMember; XmlAttributes atts = rmember.XmlAttributes; TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType); if (atts.XmlArray != null) { if (atts.XmlArray.Namespace != null && atts.XmlArray.Form == XmlSchemaForm.Unqualified) throw new InvalidOperationException ("XmlArrayAttribute.Form must not be Unqualified when it has an explicit Namespace value."); if (typeData.SchemaType != SchemaTypes.Array && !(typeData.SchemaType == SchemaTypes.Primitive && typeData.Type == typeof (byte []))) throw new InvalidOperationException ("XmlArrayAttribute can be applied to members of array or collection type."); } #if !MOONLIGHT if (atts.XmlAnyAttribute != null) { if ( (rmember.MemberType.FullName == "System.Xml.XmlAttribute[]") || (rmember.MemberType.FullName == "System.Xml.XmlNode[]") ) { mapMember = new XmlTypeMapMemberAnyAttribute(); } else throw new InvalidOperationException ("XmlAnyAttributeAttribute can only be applied to members of type XmlAttribute[] or XmlNode[]"); } else #endif if (atts.XmlAnyElements != null && atts.XmlAnyElements.Count > 0) { // no XmlNode type check is done here (seealso: bug #553032). XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement(); member.ElementInfo = ImportAnyElementInfo (defaultNamespace, rmember, member, atts); mapMember = member; } else if (atts.Xmlns) { XmlTypeMapMemberNamespaces mapNamespaces = new XmlTypeMapMemberNamespaces (); mapMember = mapNamespaces; } else if (atts.XmlAttribute != null) { // An attribute if (atts.XmlElements != null && atts.XmlElements.Count > 0) throw new Exception ("XmlAttributeAttribute and XmlElementAttribute cannot be applied to the same member"); XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute (); if (atts.XmlAttribute.AttributeName.Length == 0) mapAttribute.AttributeName = rmember.MemberName; else mapAttribute.AttributeName = atts.XmlAttribute.AttributeName; mapAttribute.AttributeName = XmlConvert.EncodeLocalName (mapAttribute.AttributeName); if (typeData.IsComplexType) mapAttribute.MappedType = ImportTypeMapping (typeData.Type, null, defaultNamespace); if (atts.XmlAttribute.Namespace != null && atts.XmlAttribute.Namespace != defaultNamespace) { if (atts.XmlAttribute.Form == XmlSchemaForm.Unqualified) throw new InvalidOperationException ("The Form property may not be 'Unqualified' when an explicit Namespace property is present"); mapAttribute.Form = XmlSchemaForm.Qualified; mapAttribute.Namespace = atts.XmlAttribute.Namespace; } else { mapAttribute.Form = atts.XmlAttribute.Form; if (atts.XmlAttribute.Form == XmlSchemaForm.Qualified) mapAttribute.Namespace = defaultNamespace; else mapAttribute.Namespace = ""; } typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.XmlAttribute.DataType); mapMember = mapAttribute; } else if (typeData.SchemaType == SchemaTypes.Array) { // If the member has a single XmlElementAttribute and the type is the type of the member, // then it is not a flat list if (atts.XmlElements.Count > 1 || (atts.XmlElements.Count == 1 && atts.XmlElements[0].Type != typeData.Type) || (atts.XmlText != null)) { // A flat list // check that it does not have XmlArrayAttribute if (atts.XmlArray != null) throw new InvalidOperationException ("XmlArrayAttribute cannot be used with members which also attributed with XmlElementAttribute or XmlTextAttribute."); XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList (); member.ListMap = new ListMap (); member.ListMap.ItemInfo = ImportElementInfo (declaringType, XmlConvert.EncodeLocalName (rmember.MemberName), defaultNamespace, typeData.ListItemType, member, atts); member.ElementInfo = member.ListMap.ItemInfo; member.ListMap.ChoiceMember = member.ChoiceMember; mapMember = member; } else { // A list XmlTypeMapMemberList member = new XmlTypeMapMemberList (); // Creates an ElementInfo that identifies the array instance. member.ElementInfo = new XmlTypeMapElementInfoList(); XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, typeData); elem.ElementName = XmlConvert.EncodeLocalName((atts.XmlArray != null && atts.XmlArray.ElementName.Length != 0) ? atts.XmlArray.ElementName : rmember.MemberName); // note that it could be changed below (when Form is Unqualified) elem.Namespace = (atts.XmlArray != null && atts.XmlArray.Namespace != null) ? atts.XmlArray.Namespace : defaultNamespace; elem.MappedType = ImportListMapping (rmember.MemberType, null, elem.Namespace, atts, 0); elem.IsNullable = (atts.XmlArray != null) ? atts.XmlArray.IsNullable : false; elem.Form = (atts.XmlArray != null) ? atts.XmlArray.Form : XmlSchemaForm.Qualified; // This is a bit tricky, but is done // after filling descendant members, so // that array items could be serialized // with proper namespace. if (atts.XmlArray != null && atts.XmlArray.Form == XmlSchemaForm.Unqualified) elem.Namespace = String.Empty; member.ElementInfo.Add (elem); mapMember = member; } } else { // An element XmlTypeMapMemberElement member = new XmlTypeMapMemberElement (); member.ElementInfo = ImportElementInfo (declaringType, XmlConvert.EncodeLocalName(rmember.MemberName), defaultNamespace, rmember.MemberType, member, atts); mapMember = member; } mapMember.DefaultValue = GetDefaultValue (typeData, atts.XmlDefaultValue); mapMember.TypeData = typeData; mapMember.Name = rmember.MemberName; mapMember.IsReturnValue = rmember.IsReturnValue; return mapMember; }
private XmlTypeMapMember CreateMapMember (XmlReflectionMember rmember, string defaultNamespace) { XmlTypeMapMember mapMember; SoapAttributes atts = rmember.SoapAttributes; TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType); if (atts.SoapAttribute != null) { // An attribute if (atts.SoapElement != null) throw new Exception ("SoapAttributeAttribute and SoapElementAttribute cannot be applied to the same member"); XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute (); if (atts.SoapAttribute.AttributeName == null) mapAttribute.AttributeName = rmember.MemberName; else mapAttribute.AttributeName = atts.SoapAttribute.AttributeName; mapAttribute.Namespace = (atts.SoapAttribute.Namespace != null) ? atts.SoapAttribute.Namespace : ""; if (typeData.IsComplexType) mapAttribute.MappedType = ImportTypeMapping (typeData.Type, defaultNamespace); typeData = TypeTranslator.GetTypeData (rmember.MemberType, atts.SoapAttribute.DataType); mapMember = mapAttribute; } else { if (typeData.SchemaType == SchemaTypes.Array) mapMember = new XmlTypeMapMemberList (); else mapMember = new XmlTypeMapMemberElement (); if (atts.SoapElement != null && atts.SoapElement.DataType != null) typeData = TypeTranslator.GetTypeData (rmember.MemberType, atts.SoapElement.DataType); // Creates an ElementInfo that identifies the element XmlTypeMapElementInfoList infoList = new XmlTypeMapElementInfoList(); XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (mapMember, typeData); elem.ElementName = (atts.SoapElement != null && atts.SoapElement.ElementName != null) ? atts.SoapElement.ElementName : rmember.MemberName; elem.Namespace = string.Empty; elem.IsNullable = (atts.SoapElement != null) ? atts.SoapElement.IsNullable : false; if (typeData.IsComplexType) elem.MappedType = ImportTypeMapping (typeData.Type, defaultNamespace); infoList.Add (elem); ((XmlTypeMapMemberElement)mapMember).ElementInfo = infoList; } mapMember.TypeData = typeData; mapMember.Name = rmember.MemberName; mapMember.IsReturnValue = rmember.IsReturnValue; return mapMember; }
public void AddMember(XmlTypeMapMember member) { // If GlobalIndex has not been set, set it now if (member.GlobalIndex == -1) { member.GlobalIndex = _allMembers.Count; } _allMembers.Add(member); if (!(member.DefaultValue is System.DBNull) && member.DefaultValue != null) { if (_membersWithDefault == null) { _membersWithDefault = new ArrayList(); } _membersWithDefault.Add(member); } if (member.IsReturnValue) { _returnMember = member; } if (member is XmlTypeMapMemberAttribute) { XmlTypeMapMemberAttribute atm = (XmlTypeMapMemberAttribute)member; if (_attributeMembers == null) { _attributeMembers = new Hashtable(); } string key = BuildKey(atm.AttributeName, atm.Namespace, -1); if (_attributeMembers.ContainsKey(key)) { throw new InvalidOperationException("The XML attribute named '" + atm.AttributeName + "' from namespace '" + atm.Namespace + "' is already present in the current scope. Use XML attributes to specify another XML name or namespace for the attribute."); } member.Index = _attributeMembers.Count; _attributeMembers.Add(key, member); return; } else if (member is XmlTypeMapMemberFlatList) { RegisterFlatList((XmlTypeMapMemberFlatList)member); } else if (member is XmlTypeMapMemberAnyElement) { XmlTypeMapMemberAnyElement mem = (XmlTypeMapMemberAnyElement)member; if (mem.IsDefaultAny) { _defaultAnyElement = mem; } if (mem.TypeData.IsListType) { RegisterFlatList(mem); } } else if (member is XmlTypeMapMemberAnyAttribute) { _defaultAnyAttribute = (XmlTypeMapMemberAnyAttribute)member; return; } else if (member is XmlTypeMapMemberNamespaces) { _namespaceDeclarations = (XmlTypeMapMemberNamespaces)member; return; } if (member is XmlTypeMapMemberElement && ((XmlTypeMapMemberElement)member).IsXmlTextCollector) { if (_xmlTextCollector != null) { throw new InvalidOperationException("XmlTextAttribute can only be applied once in a class"); } _xmlTextCollector = member; } if (_elementMembers == null) { _elementMembers = new ArrayList(); _elements = new Hashtable(); } member.Index = _elementMembers.Count; _elementMembers.Add(member); ICollection elemsInfo = ((XmlTypeMapMemberElement)member).ElementInfo; foreach (XmlTypeMapElementInfo elem in elemsInfo) { string key = BuildKey(elem.ElementName, elem.Namespace, elem.ExplicitOrder); if (_elements.ContainsKey(key)) { throw new InvalidOperationException("The XML element named '" + elem.ElementName + "' from namespace '" + elem.Namespace + "' is already present in the current scope. Use XML attributes to specify another XML name or namespace for the element."); } _elements.Add(key, elem); } if (member.TypeData.IsListType && member.TypeData.Type != null && !member.TypeData.Type.IsArray) { if (_listMembers == null) { _listMembers = new ArrayList(); } _listMembers.Add(member); } }
private XmlTypeMapMember CreateMapMember(XmlReflectionMember rmember, string defaultNamespace) { XmlTypeMapMember mapMember; SoapAttributes atts = rmember.SoapAttributes; TypeData typeData = TypeTranslator.GetTypeData(rmember.MemberType); if (atts.SoapAttribute != null) { // An attribute if (atts.SoapElement != null) { throw new Exception("SoapAttributeAttribute and SoapElementAttribute cannot be applied to the same member"); } XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute(); if (atts.SoapAttribute.AttributeName == null) { mapAttribute.AttributeName = rmember.MemberName; } else { mapAttribute.AttributeName = atts.SoapAttribute.AttributeName; } mapAttribute.Namespace = (atts.SoapAttribute.Namespace != null) ? atts.SoapAttribute.Namespace : ""; if (typeData.IsComplexType) { mapAttribute.MappedType = ImportTypeMapping(typeData.Type, defaultNamespace); } typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.SoapAttribute.DataType); mapMember = mapAttribute; } else { if (typeData.SchemaType == SchemaTypes.Array) { mapMember = new XmlTypeMapMemberList(); } else { mapMember = new XmlTypeMapMemberElement(); } if (atts.SoapElement != null && atts.SoapElement.DataType != null) { typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.SoapElement.DataType); } // Creates an ElementInfo that identifies the element XmlTypeMapElementInfoList infoList = new XmlTypeMapElementInfoList(); XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo(mapMember, typeData); elem.ElementName = (atts.SoapElement != null && atts.SoapElement.ElementName != null) ? atts.SoapElement.ElementName : rmember.MemberName; elem.Namespace = string.Empty; elem.IsNullable = (atts.SoapElement != null) ? atts.SoapElement.IsNullable : false; if (typeData.IsComplexType) { elem.MappedType = ImportTypeMapping(typeData.Type, defaultNamespace); } infoList.Add(elem); ((XmlTypeMapMemberElement)mapMember).ElementInfo = infoList; } mapMember.TypeData = typeData; mapMember.Name = rmember.MemberName; mapMember.IsReturnValue = rmember.IsReturnValue; return(mapMember); }