示例#1
0
        private void WriteElementMembers(ClassMap map, object ob, bool isValueList)
        {
            ICollection elementMembers = map.ElementMembers;

            if (elementMembers != null)
            {
                foreach (object obj in elementMembers)
                {
                    XmlTypeMapMemberElement xmlTypeMapMemberElement = (XmlTypeMapMemberElement)obj;
                    if (this.MemberHasValue(xmlTypeMapMemberElement, ob, isValueList))
                    {
                        object memberValue = this.GetMemberValue(xmlTypeMapMemberElement, ob, isValueList);
                        Type   type        = xmlTypeMapMemberElement.GetType();
                        if (type == typeof(XmlTypeMapMemberList))
                        {
                            this.WriteMemberElement((XmlTypeMapElementInfo)xmlTypeMapMemberElement.ElementInfo[0], memberValue);
                        }
                        else if (type == typeof(XmlTypeMapMemberFlatList))
                        {
                            if (memberValue != null)
                            {
                                this.WriteListContent(ob, xmlTypeMapMemberElement.TypeData, ((XmlTypeMapMemberFlatList)xmlTypeMapMemberElement).ListMap, memberValue, null);
                            }
                        }
                        else if (type == typeof(XmlTypeMapMemberAnyElement))
                        {
                            if (memberValue != null)
                            {
                                this.WriteAnyElementContent((XmlTypeMapMemberAnyElement)xmlTypeMapMemberElement, memberValue);
                            }
                        }
                        else if (type != typeof(XmlTypeMapMemberAnyAttribute))
                        {
                            if (type != typeof(XmlTypeMapMemberElement))
                            {
                                throw new InvalidOperationException("Unknown member type");
                            }
                            XmlTypeMapElementInfo elem = xmlTypeMapMemberElement.FindElement(ob, memberValue);
                            this.WriteMemberElement(elem, memberValue);
                        }
                    }
                }
            }
        }
        /// <summary>Adds an element declaration to the applicable <see cref="T:System.Xml.Schema.XmlSchema" /> for each of the element parts of a literal SOAP message definition, and specifies whether enclosing elements are included.</summary>
        /// <param name="xmlMembersMapping">The internal mapping between a .NET Framework type and an XML schema element.</param>
        /// <param name="exportEnclosingType">true if the schema elements that enclose the schema are to be included; otherwise, false.</param>
        public void ExportMembersMapping(XmlMembersMapping xmlMembersMapping, bool exportEnclosingType)
        {
            ClassMap classMap = (ClassMap)xmlMembersMapping.ObjectMap;

            if (xmlMembersMapping.HasWrapperElement && exportEnclosingType)
            {
                XmlSchema             schema = this.GetSchema(xmlMembersMapping.Namespace);
                XmlSchemaComplexType  xmlSchemaComplexType = new XmlSchemaComplexType();
                XmlSchemaSequence     particle;
                XmlSchemaAnyAttribute anyAttribute;
                this.ExportMembersMapSchema(schema, classMap, null, xmlSchemaComplexType.Attributes, out particle, out anyAttribute);
                xmlSchemaComplexType.Particle     = particle;
                xmlSchemaComplexType.AnyAttribute = anyAttribute;
                if (this.encodedFormat)
                {
                    xmlSchemaComplexType.Name = xmlMembersMapping.ElementName;
                    schema.Items.Add(xmlSchemaComplexType);
                }
                else
                {
                    XmlSchemaElement xmlSchemaElement = new XmlSchemaElement();
                    xmlSchemaElement.Name       = xmlMembersMapping.ElementName;
                    xmlSchemaElement.SchemaType = xmlSchemaComplexType;
                    schema.Items.Add(xmlSchemaElement);
                }
            }
            else
            {
                ICollection elementMembers = classMap.ElementMembers;
                if (elementMembers != null)
                {
                    foreach (object obj in elementMembers)
                    {
                        XmlTypeMapMemberElement xmlTypeMapMemberElement = (XmlTypeMapMemberElement)obj;
                        if (xmlTypeMapMemberElement is XmlTypeMapMemberAnyElement && xmlTypeMapMemberElement.TypeData.IsListType)
                        {
                            XmlSchema         schema2            = this.GetSchema(xmlMembersMapping.Namespace);
                            XmlSchemaParticle schemaArrayElement = this.GetSchemaArrayElement(schema2, xmlTypeMapMemberElement.ElementInfo);
                            if (schemaArrayElement is XmlSchemaAny)
                            {
                                XmlSchemaComplexType xmlSchemaComplexType2 = this.FindComplexType(schema2.Items, "any");
                                if (xmlSchemaComplexType2 != null)
                                {
                                    continue;
                                }
                                xmlSchemaComplexType2         = new XmlSchemaComplexType();
                                xmlSchemaComplexType2.Name    = "any";
                                xmlSchemaComplexType2.IsMixed = true;
                                XmlSchemaSequence xmlSchemaSequence = new XmlSchemaSequence();
                                xmlSchemaComplexType2.Particle = xmlSchemaSequence;
                                xmlSchemaSequence.Items.Add(schemaArrayElement);
                                schema2.Items.Add(xmlSchemaComplexType2);
                                continue;
                            }
                        }
                        XmlTypeMapElementInfo xmlTypeMapElementInfo = (XmlTypeMapElementInfo)xmlTypeMapMemberElement.ElementInfo[0];
                        XmlSchema             schema3;
                        if (this.encodedFormat)
                        {
                            schema3 = this.GetSchema(xmlMembersMapping.Namespace);
                            this.ImportNamespace(schema3, "http://schemas.xmlsoap.org/soap/encoding/");
                        }
                        else
                        {
                            schema3 = this.GetSchema(xmlTypeMapElementInfo.Namespace);
                        }
                        XmlSchemaElement xmlSchemaElement2 = this.FindElement(schema3.Items, xmlTypeMapElementInfo.ElementName);
                        XmlSchemaExporter.XmlSchemaObjectContainer container = null;
                        if (!this.encodedFormat)
                        {
                            container = new XmlSchemaExporter.XmlSchemaObjectContainer(schema3);
                        }
                        Type type = xmlTypeMapMemberElement.GetType();
                        if (xmlTypeMapMemberElement is XmlTypeMapMemberFlatList)
                        {
                            throw new InvalidOperationException("Unwrapped arrays not supported as parameters");
                        }
                        XmlSchemaElement xmlSchemaElement3;
                        if (type == typeof(XmlTypeMapMemberElement))
                        {
                            xmlSchemaElement3 = (XmlSchemaElement)this.GetSchemaElement(schema3, xmlTypeMapElementInfo, xmlTypeMapMemberElement.DefaultValue, false, container);
                        }
                        else
                        {
                            xmlSchemaElement3 = (XmlSchemaElement)this.GetSchemaElement(schema3, xmlTypeMapElementInfo, false, container);
                        }
                        if (xmlSchemaElement2 != null)
                        {
                            if (!xmlSchemaElement2.SchemaTypeName.Equals(xmlSchemaElement3.SchemaTypeName))
                            {
                                string text  = "The XML element named '" + xmlTypeMapElementInfo.ElementName + "' ";
                                string text2 = text;
                                text = string.Concat(new string[]
                                {
                                    text2,
                                    "from namespace '",
                                    schema3.TargetNamespace,
                                    "' references distinct types ",
                                    xmlSchemaElement3.SchemaTypeName.Name,
                                    " and ",
                                    xmlSchemaElement2.SchemaTypeName.Name,
                                    ". "
                                });
                                text += "Use XML attributes to specify another XML name or namespace for the element or types.";
                                throw new InvalidOperationException(text);
                            }
                            schema3.Items.Remove(xmlSchemaElement3);
                        }
                    }
                }
            }
            this.CompileSchemas();
        }
        private void ExportMembersMapSchema(XmlSchema schema, ClassMap map, XmlTypeMapping baseMap, XmlSchemaObjectCollection outAttributes, out XmlSchemaSequence particle, out XmlSchemaAnyAttribute anyAttribute)
        {
            particle = null;
            XmlSchemaSequence xmlSchemaSequence = new XmlSchemaSequence();
            ICollection       elementMembers    = map.ElementMembers;

            if (elementMembers != null && !map.HasSimpleContent)
            {
                foreach (object obj in elementMembers)
                {
                    XmlTypeMapMemberElement xmlTypeMapMemberElement = (XmlTypeMapMemberElement)obj;
                    if (baseMap == null || !this.DefinedInBaseMap(baseMap, xmlTypeMapMemberElement))
                    {
                        Type type = xmlTypeMapMemberElement.GetType();
                        if (type == typeof(XmlTypeMapMemberFlatList))
                        {
                            XmlSchemaParticle schemaArrayElement = this.GetSchemaArrayElement(schema, xmlTypeMapMemberElement.ElementInfo);
                            if (schemaArrayElement != null)
                            {
                                xmlSchemaSequence.Items.Add(schemaArrayElement);
                            }
                        }
                        else if (type == typeof(XmlTypeMapMemberAnyElement))
                        {
                            xmlSchemaSequence.Items.Add(this.GetSchemaArrayElement(schema, xmlTypeMapMemberElement.ElementInfo));
                        }
                        else if (type == typeof(XmlTypeMapMemberElement))
                        {
                            this.GetSchemaElement(schema, (XmlTypeMapElementInfo)xmlTypeMapMemberElement.ElementInfo[0], xmlTypeMapMemberElement.DefaultValue, true, new XmlSchemaExporter.XmlSchemaObjectContainer(xmlSchemaSequence));
                        }
                        else
                        {
                            this.GetSchemaElement(schema, (XmlTypeMapElementInfo)xmlTypeMapMemberElement.ElementInfo[0], true, new XmlSchemaExporter.XmlSchemaObjectContainer(xmlSchemaSequence));
                        }
                    }
                }
            }
            if (xmlSchemaSequence.Items.Count > 0)
            {
                particle = xmlSchemaSequence;
            }
            ICollection attributeMembers = map.AttributeMembers;

            if (attributeMembers != null)
            {
                foreach (object obj2 in attributeMembers)
                {
                    XmlTypeMapMemberAttribute xmlTypeMapMemberAttribute = (XmlTypeMapMemberAttribute)obj2;
                    if (baseMap == null || !this.DefinedInBaseMap(baseMap, xmlTypeMapMemberAttribute))
                    {
                        outAttributes.Add(this.GetSchemaAttribute(schema, xmlTypeMapMemberAttribute, true));
                    }
                }
            }
            XmlTypeMapMember defaultAnyAttributeMember = map.DefaultAnyAttributeMember;

            if (defaultAnyAttributeMember != null)
            {
                anyAttribute = new XmlSchemaAnyAttribute();
            }
            else
            {
                anyAttribute = null;
            }
        }