Esempio n. 1
0
        public void AddMappingMetadata(CodeAttributeDeclarationCollection metadata, XmlMemberMapping member, string ns, bool forceUseMemberName)
        {
            CodeAttributeDeclaration att;
            TypeData memType = member.TypeMapMember.TypeData;

            if (member.Any)
            {
                XmlTypeMapElementInfoList list = (XmlTypeMapElementInfoList)((XmlTypeMapMemberElement)member.TypeMapMember).ElementInfo;
                foreach (XmlTypeMapElementInfo info in list)
                {
                    if (info.IsTextElement)
                    {
                        metadata.Add(new CodeAttributeDeclaration("Mpd.Xml.Serialization.XmlText"));
                    }
                    else
                    {
                        att = new CodeAttributeDeclaration("Mpd.Xml.Serialization.XmlAnyElement");
                        if (!info.IsUnnamedAnyElement)
                        {
                            att.Arguments.Add(MapCodeGenerator.GetArg("Name", info.ElementName));
                            if (info.Namespace != ns)
                            {
                                att.Arguments.Add(MapCodeGenerator.GetArg("Namespace", member.Namespace));
                            }
                        }
                        metadata.Add(att);
                    }
                }
            }
            else if (member.TypeMapMember is XmlTypeMapMemberList)
            {
                // Array parameter
                XmlTypeMapMemberList list = member.TypeMapMember as XmlTypeMapMemberList;
                ListMap listMap           = (ListMap)list.ListTypeMapping.ObjectMap;

                codeGenerator.AddArrayAttributes(metadata, list, ns, forceUseMemberName);
                codeGenerator.AddArrayItemAttributes(metadata, listMap, memType.ListItemTypeData, list.Namespace, 0);
            }
            else if (member.TypeMapMember is XmlTypeMapMemberElement)
            {
                codeGenerator.AddElementMemberAttributes((XmlTypeMapMemberElement)member.TypeMapMember, ns, metadata, forceUseMemberName);
            }
            else if (member.TypeMapMember is XmlTypeMapMemberAttribute)
            {
                codeGenerator.AddAttributeMemberAttributes((XmlTypeMapMemberAttribute)member.TypeMapMember, ns, metadata, forceUseMemberName);
            }
            else
            {
                throw new NotSupportedException("Schema type not supported");
            }
        }
Esempio n. 2
0
        XmlTypeMapping ImportListMapping(TypeData typeData, string defaultNamespace)
        {
            Type type = typeData.Type;

            XmlTypeMapping map = helper.GetRegisteredClrType(type, XmlSerializer.EncodingNamespace);

            if (map != null)
            {
                return(map);
            }

            ListMap  obmap        = new ListMap();
            TypeData itemTypeData = typeData.ListItemTypeData;

            map = CreateTypeMapping(typeData, "Array", XmlSerializer.EncodingNamespace);
            helper.RegisterClrType(map, type, XmlSerializer.EncodingNamespace);
            map.MultiReferenceType = true;
            map.ObjectMap          = obmap;

            XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo(null, itemTypeData);

            if (elem.TypeData.IsComplexType)
            {
                elem.MappedType = ImportTypeMapping(typeData.ListItemType, defaultNamespace);
                elem.TypeData   = elem.MappedType.TypeData;
            }

            elem.ElementName = "Item";
            elem.Namespace   = string.Empty;
            elem.IsNullable  = true;            // By default, items are nullable

            XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();

            list.Add(elem);

            obmap.ItemInfo = list;
            XmlTypeMapping objMap = ImportTypeMapping(typeof(object), defaultNamespace);

            objMap.DerivedTypes.Add(map);

            // Register any of the including types as a derived class of object
            SoapIncludeAttribute[] includes = (SoapIncludeAttribute[])type.GetCustomAttributes(typeof(SoapIncludeAttribute), false);
            for (int i = 0; i < includes.Length; i++)
            {
                Type includedType = includes[i].Type;
                objMap.DerivedTypes.Add(ImportTypeMapping(includedType, defaultNamespace));
            }

            return(map);
        }
Esempio n. 3
0
        XmlSchemaParticle GetSchemaArrayElement(XmlSchema currentSchema, XmlTypeMapElementInfoList infos)
        {
            int numInfos = infos.Count;

            if (numInfos > 0 && ((XmlTypeMapElementInfo)infos[0]).IsTextElement)
            {
                numInfos--;
            }
            if (numInfos == 0)
            {
                return(null);
            }

            if (numInfos == 1)
            {
                XmlSchemaParticle selem = GetSchemaElement(currentSchema, (XmlTypeMapElementInfo)infos[infos.Count - 1], true);
                selem.MinOccursString = "0";
                selem.MaxOccursString = "unbounded";
                return(selem);
            }
            else
            {
                XmlSchemaChoice schoice = new XmlSchemaChoice();
                schoice.MinOccursString = "0";
                schoice.MaxOccursString = "unbounded";
                foreach (XmlTypeMapElementInfo einfo in infos)
                {
                    if (einfo.IsTextElement)
                    {
                        continue;
                    }
                    schoice.Items.Add(GetSchemaElement(currentSchema, einfo, true));
                }
                return(schoice);
            }
        }
Esempio n. 4
0
        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);
        }