public XmlTypeMapping ImportTypeMapping(Type type, string defaultNamespace) { if (type == null) { throw new ArgumentNullException("type"); } if (type == typeof(void)) { throw new InvalidOperationException("Type " + type.Name + " may not be serialized."); } return(ImportTypeMapping(TypeTranslator.GetTypeData(type), defaultNamespace)); }
ICollection GetReflectionMembers(Type type) { ArrayList members = new ArrayList(); PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (PropertyInfo prop in properties) { if (!prop.CanRead) { continue; } if (!prop.CanWrite && (TypeTranslator.GetTypeData(prop.PropertyType).SchemaType != SchemaTypes.Array || prop.PropertyType.IsArray)) { continue; } SoapAttributes atts = attributeOverrides[type, prop.Name]; if (atts == null) { atts = new SoapAttributes(prop); } if (atts.SoapIgnore) { continue; } XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts); members.Add(member); } FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public); foreach (FieldInfo field in fields) { SoapAttributes atts = attributeOverrides[type, field.Name]; if (atts == null) { atts = new SoapAttributes(field); } if (atts.SoapIgnore) { continue; } XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts); members.Add(member); } return(members); }
protected override void GenerateElementInfoMember(CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, XmlTypeMapElementInfo einfo, TypeData defaultType, string defaultNamespace, bool addAlwaysAttr, bool forceUseMemberName) { CodeAttributeDeclaration att = new CodeAttributeDeclaration("Mono.System.Xml.Serialization.SoapElement"); if (forceUseMemberName || einfo.ElementName != member.Name) { att.Arguments.Add(GetArg(einfo.ElementName)); } // if (einfo.IsNullable) att.Arguments.Add (GetArg ("IsNullable", true)); MS seems to ignore this if (!TypeTranslator.IsDefaultPrimitiveTpeData(einfo.TypeData)) { att.Arguments.Add(GetArg("DataType", einfo.TypeData.XmlType)); } if (addAlwaysAttr || att.Arguments.Count > 0) { attributes.Add(att); } }
protected override void GenerateAttributeMember(CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberAttribute attinfo, string defaultNamespace, bool forceUseMemberName) { CodeAttributeDeclaration att = new CodeAttributeDeclaration("Mono.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); }
protected void WriteTypedPrimitive(string name, string ns, object o, bool xsiType) { string value; TypeData td = TypeTranslator.GetTypeData(o.GetType(), null, true); if (td.SchemaType != SchemaTypes.Primitive) { throw new InvalidOperationException(String.Format("The type of the argument object '{0}' is not primitive.", td.FullTypeName)); } if (name == null) { ns = td.IsXsdType ? XmlSchema.Namespace : XmlSerializer.WsdlTypesNamespace; name = td.XmlType; } else { name = XmlCustomFormatter.FromXmlName(name); } Writer.WriteStartElement(name, ns); if (o is XmlQualifiedName) { value = FromXmlQualifiedName((XmlQualifiedName)o); } else { value = XmlCustomFormatter.ToXmlString(td, o); } if (xsiType) { if (td.SchemaType != SchemaTypes.Primitive) { throw new InvalidOperationException(string.Format(unexpectedTypeError, o.GetType().FullName)); } WriteXsiType(td.XmlType, td.IsXsdType ? XmlSchema.Namespace : XmlSerializer.WsdlTypesNamespace); } WriteValue(value); Writer.WriteEndElement(); }
public void AddMappingMetadata(CodeAttributeDeclarationCollection metadata, XmlMemberMapping member, bool forceUseMemberName) { TypeData memType = member.TypeMapMember.TypeData; CodeAttributeDeclaration att = new CodeAttributeDeclaration("Mono.System.Xml.Serialization.SoapElement"); if (forceUseMemberName || (member.ElementName != member.MemberName)) { att.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(member.ElementName))); } if (!TypeTranslator.IsDefaultPrimitiveTpeData(memType)) { att.Arguments.Add(new CodeAttributeArgument("DataType", new CodePrimitiveExpression(member.TypeName))); } if (att.Arguments.Count > 0) { metadata.Add(att); } }
internal void AddKeyHash(global::System.Text.StringBuilder sb) { sb.Append("SA "); if (soapIgnore) { sb.Append('i'); } if (soapAttribute != null) { soapAttribute.AddKeyHash(sb); } if (soapElement != null) { soapElement.AddKeyHash(sb); } if (soapEnum != null) { soapEnum.AddKeyHash(sb); } if (soapType != null) { soapType.AddKeyHash(sb); } if (soapDefaultValue == null) { sb.Append("n"); } else if (!(soapDefaultValue is global::System.DBNull)) { string v = XmlCustomFormatter.ToXmlString(TypeTranslator.GetTypeData(soapDefaultValue.GetType()), soapDefaultValue); sb.Append("v" + v); } sb.Append("|"); }
protected void WriteReferencedElements() { if (referencedElements == null) { return; } if (callbacks == null) { return; } while (referencedElements.Count > 0) { object o = referencedElements.Dequeue(); TypeData td = TypeTranslator.GetTypeData(o.GetType()); WriteCallbackInfo info = (WriteCallbackInfo)callbacks[o.GetType()]; if (info != null) { WriteStartElement(info.TypeName, info.TypeNs, true); Writer.WriteAttributeString("id", GetId(o, false)); if (td.SchemaType != SchemaTypes.Array) // Array use its own "arrayType" attribute { WriteXsiType(info.TypeName, info.TypeNs); } info.Callback(o); WriteEndElement(); } else if (IsPrimitiveArray(td)) { WriteArray(o, td); } } }
protected void WriteXmlAttribute(XmlNode node, object container) { XmlAttribute attr = node as XmlAttribute; if (attr == null) { throw new InvalidOperationException("The node must be either type XmlAttribute or a derived type."); } if (attr.NamespaceURI == XmlSerializer.WsdlNamespace) { // The wsdl arrayType attribute needs special handling if (attr.LocalName == "arrayType") { string ns, type, dimensions; TypeTranslator.ParseArrayType(attr.Value, out type, out ns, out dimensions); string value = GetQualifiedName(type + dimensions, ns); WriteAttribute(attr.Prefix, attr.LocalName, attr.NamespaceURI, value); return; } } WriteAttribute(attr.Prefix, attr.LocalName, attr.NamespaceURI, attr.Value); }
XmlSchemaAttribute GetSchemaAttribute(XmlSchema currentSchema, XmlTypeMapMemberAttribute attinfo, bool isTypeMember) { XmlSchemaAttribute sat = new XmlSchemaAttribute(); if (attinfo.DefaultValue != global::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); }
bool ReadList(out object resultList) { string arrayTypeAttr = Reader.GetAttribute(arrayType, soapNS); if (arrayTypeAttr == null) { arrayTypeAttr = Reader.GetAttribute(arrayType, wsdlNS); } XmlQualifiedName qn = ToXmlQualifiedName(arrayTypeAttr); int i = qn.Name.LastIndexOf('['); string dim = qn.Name.Substring(i); string itemType = qn.Name.Substring(0, i); int count = Int32.Parse(dim.Substring(1, dim.Length - 2), CultureInfo.InvariantCulture); Array list; i = itemType.IndexOf('['); if (i == -1) { i = itemType.Length; } string baseType = itemType.Substring(0, i); string arrayTypeName; if (qn.Namespace == w3SchemaNS) { arrayTypeName = TypeTranslator.GetPrimitiveTypeData(baseType).Type.FullName + itemType.Substring(i); } else { WriteCallbackInfo info = GetCallbackInfo(new XmlQualifiedName(baseType, qn.Namespace)); arrayTypeName = info.Type.FullName + itemType.Substring(i) + ", " + info.Type.Assembly.FullName; } list = Array.CreateInstance(Type.GetType(arrayTypeName), count); bool listComplete = true; if (Reader.IsEmptyElement) { readCount++; Reader.Skip(); } else { Reader.ReadStartElement(); for (int n = 0; n < count; n++) { whileIterationCount++; readCount++; Reader.MoveToContent(); string id; object item = ReadReferencingElement(itemType, qn.Namespace, out id); if (id == null) { list.SetValue(item, n); } else { AddFixup(new CollectionItemFixup(list, n, id)); listComplete = false; } } whileIterationCount = 0; Reader.ReadEndElement(); } resultList = list; return(listComplete); }
protected void WritePotentiallyReferencingElement(string n, string ns, object o, Type ambientType, bool suppressReference, bool isNullable) { if (o == null) { if (isNullable) { WriteNullTagEncoded(n, ns); } return; } var t = o.GetType(); WriteStartElement(n, ns, true); CheckReferenceQueue(); if (callbacks != null && callbacks.ContainsKey(o.GetType())) { WriteCallbackInfo info = (WriteCallbackInfo)callbacks[t]; if (t.IsEnum) { info.Callback(o); } else if (suppressReference) { Writer.WriteAttributeString("id", GetId(o, false)); if (ambientType != t) { WriteXsiType(info.TypeName, info.TypeNs); } info.Callback(o); } else { if (!AlreadyQueued(o)) { referencedElements.Enqueue(o); } Writer.WriteAttributeString("href", "#" + GetId(o, true)); } } else { // Must be a primitive type or array of primitives TypeData td = TypeTranslator.GetTypeData(t, null, true); if (td.SchemaType == SchemaTypes.Primitive) { if (t != ambientType) { WriteXsiType(td.XmlType, XmlSchema.Namespace); } Writer.WriteString(XmlCustomFormatter.ToXmlString(td, o)); } else if (IsPrimitiveArray(td)) { if (!AlreadyQueued(o)) { referencedElements.Enqueue(o); } Writer.WriteAttributeString("href", "#" + GetId(o, true)); } else { throw new InvalidOperationException("Invalid type: " + t.FullName); } } WriteEndElement(); }
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); }
XmlTypeMapping ImportClassMapping(Type type, string defaultNamespace) { TypeData typeData = TypeTranslator.GetTypeData(type); return(ImportClassMapping(typeData, defaultNamespace)); }