Beispiel #1
0
        private MemberMapping ImportFieldMapping(FieldModel model, SoapAttributes a, string ns, RecursionLimiter limiter)
        {
            if (a.SoapIgnore)
            {
                return(null);
            }
            MemberMapping member = new MemberMapping();

            member.IsSoap                       = true;
            member.Name                         = model.Name;
            member.CheckShouldPersist           = model.CheckShouldPersist;
            member.CheckSpecified               = model.CheckSpecified;
            member.MemberInfo                   = model.MemberInfo;
            member.CheckSpecifiedMemberInfo     = model.CheckSpecifiedMemberInfo;
            member.CheckShouldPersistMethodInfo = model.CheckShouldPersistMethodInfo;
            member.ReadOnly                     = model.ReadOnly; // || !model.FieldTypeDesc.HasDefaultConstructor;
            ImportAccessorMapping(member, model, a, ns, XmlSchemaForm.Unqualified, limiter);
            return(member);
        }
Beispiel #2
0
        private EnumMapping ImportEnumMapping(EnumModel model)
        {
            SoapAttributes a      = GetAttributes(model.Type);
            string         typeNs = _defaultNs;

            if (a.SoapType != null && a.SoapType.Namespace != null)
            {
                typeNs = a.SoapType.Namespace;
            }
            string typeName = XsdTypeName(model.Type, a, model.TypeDesc.Name);

            typeName = XmlConvert.EncodeLocalName(typeName);

            EnumMapping mapping = (EnumMapping)GetTypeMapping(typeName, typeNs, model.TypeDesc);

            if (mapping == null)
            {
                mapping           = new EnumMapping();
                mapping.IsSoap    = true;
                mapping.TypeDesc  = model.TypeDesc;
                mapping.TypeName  = typeName;
                mapping.Namespace = typeNs;
                mapping.IsFlags   = model.Type.IsDefined(typeof(FlagsAttribute), false);
                _typeScope.AddTypeMapping(mapping);
                _types.Add(typeName, typeNs, mapping);
                ArrayList constants = new ArrayList();
                for (int i = 0; i < model.Constants.Length; i++)
                {
                    ConstantMapping constant = ImportConstantMapping(model.Constants[i]);
                    if (constant != null)
                    {
                        constants.Add(constant);
                    }
                }
                if (constants.Count == 0)
                {
                    throw new InvalidOperationException(SR.Format(SR.XmlNoSerializableMembers, model.TypeDesc.FullName));
                }
                mapping.Constants = (ConstantMapping[])constants.ToArray(typeof(ConstantMapping));
            }
            return(mapping);
        }
    private void SerializeOverride(string fileName)
    {
        SoapAttributeOverrides soapOver = new SoapAttributeOverrides();
        SoapAttributes         SoapAtts = new SoapAttributes();

        // Add a SoapEnumAttribute for the GroupType.A enumerator.
        // Instead of 'A'  it will be "West".
        SoapEnumAttribute soapEnum = new SoapEnumAttribute("West");

        // Override the "A" enumerator.
        SoapAtts.SoapEnum = soapEnum;
        soapOver.Add(typeof(GroupType), "A", SoapAtts);

        // Add another SoapEnumAttribute for the GroupType.B enumerator.
        // Instead of //B// it will be "East".
        SoapAtts          = new SoapAttributes();
        soapEnum          = new SoapEnumAttribute();
        soapEnum.Name     = "East";
        SoapAtts.SoapEnum = soapEnum;
        soapOver.Add(typeof(GroupType), "B", SoapAtts);

        // Create an XmlSerializer used for overriding.
        XmlTypeMapping map =
            new SoapReflectionImporter(soapOver).
            ImportTypeMapping(typeof(Group));
        XmlSerializer ser     = new XmlSerializer(map);
        Group         myGroup = new Group();

        myGroup.GroupName = ".NET";
        myGroup.Grouptype = GroupType.B;
        // Writing the file requires a TextWriter.
        TextWriter writer = new StreamWriter(fileName);

        ser.Serialize(writer, myGroup);
        writer.Close();
    }
Beispiel #4
0
    private XmlSerializer CreateOverrideSerializer()
    {
        SoapAttributeOverrides mySoapAttributeOverrides =
            new SoapAttributeOverrides();
        SoapAttributes soapAtts = new SoapAttributes();
        // Create a new DefaultValueAttribute object for the GroupName
        // property.
        DefaultValueAttribute newDefault =
            new DefaultValueAttribute("Team1");

        soapAtts.SoapDefaultValue = newDefault;

        mySoapAttributeOverrides.Add(typeof(Group), "GroupName",
                                     soapAtts);

        // Create an XmlTypeMapping that is used to create an instance
        // of the XmlSerializer. Then return the XmlSerializer object.
        XmlTypeMapping myMapping = (new SoapReflectionImporter(
                                        mySoapAttributeOverrides)).ImportTypeMapping(typeof(Group));

        XmlSerializer ser = new XmlSerializer(myMapping);

        return(ser);
    }
        string GetTypeNamespace(TypeData typeData, string defaultNamespace)
        {
            string membersNamespace = defaultNamespace;

            SoapAttributes atts = null;

            if (!typeData.IsListType)
            {
                if (attributeOverrides != null)
                {
                    atts = attributeOverrides[typeData.Type];
                }
            }

            if (atts == null)
            {
                atts = new SoapAttributes(typeData.Type);
            }

            if (atts.SoapType != null)
            {
                if (atts.SoapType.Namespace != null && atts.SoapType.Namespace != string.Empty)
                {
                    membersNamespace = atts.SoapType.Namespace;
                }
            }

            if (membersNamespace == null)
            {
                return("");
            }
            else
            {
                return(membersNamespace);
            }
        }
Beispiel #6
0
        private ConstantMapping ImportConstantMapping(ConstantModel model)
        {
            SoapAttributes a = GetAttributes(model.FieldInfo);

            if (a.SoapIgnore)
            {
                return(null);
            }
            if ((a.GetSoapFlags() & ~SoapAttributeFlags.Enum) != 0)
            {
                throw new InvalidOperationException(SR.XmlInvalidEnumAttribute);
            }
            if (a.SoapEnum == null)
            {
                a.SoapEnum = new SoapEnumAttribute();
            }

            ConstantMapping constant = new ConstantMapping();

            constant.XmlName = a.SoapEnum.Name.Length == 0 ? model.Name : a.SoapEnum.Name;
            constant.Name    = model.Name;
            constant.Value   = model.Value;
            return(constant);
        }
Beispiel #7
0
        private TypeMapping ImportTypeMapping(TypeModel model, string dataType, RecursionLimiter limiter)
        {
            if (dataType.Length > 0)
            {
                if (!model.TypeDesc.IsPrimitive)
                {
                    throw new InvalidOperationException(SR.Format(SR.XmlInvalidDataTypeUsage, dataType, "SoapElementAttribute.DataType"));
                }
                TypeDesc td = _typeScope.GetTypeDesc(dataType, XmlSchema.Namespace);
                if (td == null)
                {
                    throw new InvalidOperationException(SR.Format(SR.XmlInvalidXsdDataType, dataType, "SoapElementAttribute.DataType", new XmlQualifiedName(dataType, XmlSchema.Namespace).ToString()));
                }
                if (model.TypeDesc.FullName != td.FullName)
                {
                    throw new InvalidOperationException(SR.Format(SR.XmlDataTypeMismatch, dataType, "SoapElementAttribute.DataType", model.TypeDesc.FullName));
                }
            }

            SoapAttributes a = GetAttributes(model.Type);

            if ((a.GetSoapFlags() & ~SoapAttributeFlags.Type) != 0)
            {
                throw new InvalidOperationException(SR.Format(SR.XmlInvalidTypeAttributes, model.Type.FullName));
            }

            switch (model.TypeDesc.Kind)
            {
            case TypeKind.Enum:
                return(ImportEnumMapping((EnumModel)model));

            case TypeKind.Primitive:
                return(ImportPrimitiveMapping((PrimitiveModel)model, dataType));

            case TypeKind.Array:
            case TypeKind.Collection:
            case TypeKind.Enumerable:
                return(ImportArrayLikeMapping((ArrayModel)model, limiter));

            case TypeKind.Root:
            case TypeKind.Class:
            case TypeKind.Struct:
                if (model.TypeDesc.IsOptionalValue)
                {
                    TypeDesc       baseTypeDesc   = model.TypeDesc.BaseTypeDesc;
                    SoapAttributes baseAttributes = GetAttributes(baseTypeDesc.Type);
                    string         typeNs         = _defaultNs;
                    if (baseAttributes.SoapType != null && baseAttributes.SoapType.Namespace != null)
                    {
                        typeNs = baseAttributes.SoapType.Namespace;
                    }
                    TypeDesc    valueTypeDesc = string.IsNullOrEmpty(dataType) ? model.TypeDesc.BaseTypeDesc : _typeScope.GetTypeDesc(dataType, XmlSchema.Namespace);
                    string      xsdTypeName   = string.IsNullOrEmpty(dataType) ? model.TypeDesc.BaseTypeDesc.Name : dataType;
                    TypeMapping baseMapping   = GetTypeMapping(xsdTypeName, typeNs, valueTypeDesc);
                    if (baseMapping == null)
                    {
                        baseMapping = ImportTypeMapping(_modelScope.GetTypeModel(baseTypeDesc.Type), dataType, limiter);
                    }
                    return(CreateNullableMapping(baseMapping, model.TypeDesc.Type));
                }
                else
                {
                    return(ImportStructLikeMapping((StructModel)model, limiter));
                }

            default:
                throw new NotSupportedException(SR.Format(SR.XmlUnsupportedSoapTypeKind, model.TypeDesc.FullName));
            }
        }
 public void Add(Type type, string member, SoapAttributes attributes)
 {
 }
 // Methods
 public void Add(Type type, SoapAttributes attributes)
 {
 }
Beispiel #10
0
 /// <include file='doc\SoapAttributeOverrides.uex' path='docs/doc[@for="SoapAttributeOverrides.Add"]/*' />
 /// <devdoc>
 ///    <para>[To be supplied.]</para>
 /// </devdoc>
 public void Add(Type type, SoapAttributes attributes)
 {
     Add(type, string.Empty, attributes);
 }
Beispiel #11
0
 public void Add(Type type, string member, SoapAttributes attributes)
 {
 }
Beispiel #12
0
        private StructMapping ImportStructLikeMapping(StructModel model, RecursionLimiter limiter)
        {
            if (model.TypeDesc.Kind == TypeKind.Root)
            {
                return(GetRootMapping());
            }

            SoapAttributes a = GetAttributes(model.Type);

            string typeNs = _defaultNs;

            if (a.SoapType != null && a.SoapType.Namespace != null)
            {
                typeNs = a.SoapType.Namespace;
            }
            string typeName = XsdTypeName(model.Type, a, model.TypeDesc.Name);

            typeName = XmlConvert.EncodeLocalName(typeName);

            StructMapping mapping = (StructMapping)GetTypeMapping(typeName, typeNs, model.TypeDesc);

            if (mapping == null)
            {
                mapping           = new StructMapping();
                mapping.IsSoap    = true;
                mapping.TypeDesc  = model.TypeDesc;
                mapping.Namespace = typeNs;
                mapping.TypeName  = typeName;
                if (a.SoapType != null)
                {
                    mapping.IncludeInSchema = a.SoapType.IncludeInSchema;
                }
                _typeScope.AddTypeMapping(mapping);
                _types.Add(typeName, typeNs, mapping);
                if (limiter.IsExceededLimit)
                {
                    limiter.DeferredWorkItems.Add(new ImportStructWorkItem(model, mapping));
                    return(mapping);
                }

                limiter.Depth++;

                InitializeStructMembers(mapping, model, limiter);
                while (limiter.DeferredWorkItems.Count > 0)
                {
                    int index = limiter.DeferredWorkItems.Count - 1;
                    ImportStructWorkItem item = limiter.DeferredWorkItems[index];
                    if (InitializeStructMembers(item.Mapping, item.Model, limiter))
                    {
                        //
                        // if InitializeStructMembers returns true, then there were *no* changes to the DeferredWorkItems
                        //
#if DEBUG
                        // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
                        if (index != limiter.DeferredWorkItems.Count - 1)
                        {
                            throw new InvalidOperationException(SR.Format(SR.XmlInternalErrorDetails, "DeferredWorkItems.Count have changed"));
                        }
                        if (item != limiter.DeferredWorkItems[index])
                        {
                            throw new InvalidOperationException(SR.Format(SR.XmlInternalErrorDetails, "DeferredWorkItems.Top have changed"));
                        }
#endif
                        // Remove the last work item
                        limiter.DeferredWorkItems.RemoveAt(index);
                    }
                }
                limiter.Depth--;
            }
            return(mapping);
        }
Beispiel #13
0
        private bool InitializeStructMembers(StructMapping mapping, StructModel model, RecursionLimiter limiter)
        {
            if (mapping.IsFullyInitialized)
            {
                return(true);
            }
            if (model.TypeDesc.BaseTypeDesc != null)
            {
                StructMapping baseMapping = ImportStructLikeMapping((StructModel)_modelScope.GetTypeModel(model.Type.BaseType, false), limiter);

                // check to see if the import of the baseMapping was deferred
                int baseIndex = limiter.DeferredWorkItems.IndexOf(mapping.BaseMapping);
                if (baseIndex < 0)
                {
                    mapping.BaseMapping = baseMapping;
                }
                else
                {
                    // the import of the baseMapping was deferred, make sure that the derived mappings is deferred as well
                    if (!limiter.DeferredWorkItems.Contains(mapping))
                    {
                        limiter.DeferredWorkItems.Add(new ImportStructWorkItem(model, mapping));
                    }
                    // make sure that baseMapping get processed before the derived
                    int top = limiter.DeferredWorkItems.Count - 1;
                    if (baseIndex < top)
                    {
                        ImportStructWorkItem baseMappingWorkItem = limiter.DeferredWorkItems[baseIndex];
                        limiter.DeferredWorkItems[baseIndex] = limiter.DeferredWorkItems[top];
                        limiter.DeferredWorkItems[top]       = baseMappingWorkItem;
                    }
                    return(false);
                }
            }
            ArrayList members = new ArrayList();

            foreach (MemberInfo memberInfo in model.GetMemberInfos())
            {
                if (!(memberInfo is FieldInfo) && !(memberInfo is PropertyInfo))
                {
                    continue;
                }
                SoapAttributes memberAttrs = GetAttributes(memberInfo);
                if (memberAttrs.SoapIgnore)
                {
                    continue;
                }
                FieldModel fieldModel = model.GetFieldModel(memberInfo);
                if (fieldModel == null)
                {
                    continue;
                }
                MemberMapping member = ImportFieldMapping(fieldModel, memberAttrs, mapping.Namespace, limiter);
                if (member == null)
                {
                    continue;
                }

                if (!member.TypeDesc.IsPrimitive && !member.TypeDesc.IsEnum && !member.TypeDesc.IsOptionalValue)
                {
                    if (model.TypeDesc.IsValueType)
                    {
                        throw new NotSupportedException(SR.Format(SR.XmlRpcRefsInValueType, model.TypeDesc.FullName));
                    }
                    if (member.TypeDesc.IsValueType)
                    {
                        throw new NotSupportedException(SR.Format(SR.XmlRpcNestedValueType, member.TypeDesc.FullName));
                    }
                }
                if (mapping.BaseMapping != null)
                {
                    if (mapping.BaseMapping.Declares(member, mapping.TypeName))
                    {
                        continue;
                    }
                }
                members.Add(member);
            }
            mapping.Members = (MemberMapping[])members.ToArray(typeof(MemberMapping));
            if (mapping.BaseMapping == null)
            {
                mapping.BaseMapping = GetRootMapping();
            }
            IncludeTypes(model.Type, limiter);

            return(true);
        }
        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 static string ObjectToXmlString_second(Object _object, Type objType)
        {
            // Create and return an XmlSerializer instance used to
            // override and create SOAP messages.
            SoapAttributeOverrides mySoapAttributeOverrides = new SoapAttributeOverrides();
            SoapAttributes         soapAtts = new SoapAttributes();

            // Override the SoapTypeAttribute.
            SoapTypeAttribute soapType = new SoapTypeAttribute();

            //soapType.TypeName = "Team";
            soapType.IncludeInSchema = false;
            //soapType.Namespace = "http://www.microsoft.com";

            // https://docs.microsoft.com/en-us/dotnet/api/system.xml.serialization.soaptypeattribute.includeinschema?view=netframework-4.8
            // https://docs.microsoft.com/en-us/dotnet/standard/data/xml/
            // https://stackoverflow.com/questions/1729711/prevent-xmlserializer-from-emitting-xsitype-on-inherited-types/1730412#1730412
            // https://stackoverflow.com/questions/1729711/prevent-xmlserializer-from-emitting-xsitype-on-inherited-types/1730412
            // https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/dkwy2d72(v=vs.100)
            // https://csharp.hotexamples.com/examples/System.Xml/XmlWriterSettings/-/php-xmlwritersettings-class-examples.html

            //mySoapAttributeOverrides.Add(typeof(QuoteData), soapAtts);

            mySoapAttributeOverrides.Add(objType, soapAtts);


            // Serializes a class named Group as a SOAP message.
            XmlTypeMapping myTypeMapping =
                new SoapReflectionImporter(mySoapAttributeOverrides).ImportTypeMapping(objType);


            XmlSerializer mySerializer = new XmlSerializer(myTypeMapping);
            //XmlSerializer mySerializer = new XmlSerializer(objType);

            XmlWriterSettings settings = new XmlWriterSettings();


            // this removes <?xml version="1.0" encoding="utf-16"?>
            //settings.ConformanceLevel = ConformanceLevel.Document;
            //settings.ConformanceLevel = ConformanceLevel.Auto;

            settings.OmitXmlDeclaration = true;

            settings.Indent = false;

            // this removes <?xml version="1.0" encoding="utf-16"?>
            //settings.OmitXmlDeclaration = true;

            settings.NewLineChars    = string.Empty;
            settings.NewLineHandling = NewLineHandling.None;

            string xmlStr = string.Empty;

            using (StringWriter stringWriter = new StringWriter())
            {
                using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, settings))
                {
                    mySerializer.Serialize(xmlWriter, _object);

                    xmlStr = stringWriter.ToString();
                    xmlWriter.Close();
                }
                stringWriter.Close();
            }

            return(xmlStr);
        }
 internal XmlReflectionMember(string name, Type type, SoapAttributes attributes)
 {
     memberName     = name;
     memberType     = type;
     soapAttributes = attributes;
 }
Beispiel #17
0
		internal XmlReflectionMember (string name, Type type, SoapAttributes attributes)
		{
			memberName = name;
			memberType = type;
			soapAttributes = attributes;
		}
Beispiel #18
0
 // Methods
 public void Add(Type type, SoapAttributes attributes)
 {
 }