コード例 #1
0
        internal void WriteXml(XmlWriter writer, object value, XmlMember member, XmlTypeContext typeContext)
        {
            var lastMember   = Member;
            var lastContract = Contract;

            Member   = member;
            Contract = typeContext.Contract;

            typeContext.WriteXml(writer, value, this);

            Member   = lastMember;
            Contract = lastContract;
        }
コード例 #2
0
        internal object ReadXml(XmlReader reader, XmlMember member, XmlTypeContext typeContext)
        {
            var lastMember   = Member;
            var lastContract = Contract;

            Member   = member;
            Contract = typeContext.Contract;

            var value = typeContext.ReadXml(reader, this);

            Member   = lastMember;
            Contract = lastContract;

            return(value);
        }
コード例 #3
0
        private void Serialize(XmlWriter writer, object value, Type memberType, XmlMember member)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            if (value == null)
            {
                WriteNull(writer, memberType, member);
                return;
            }

            XmlTypeContext context = null;

            if (member == null)
            {
                context = Settings.GetTypeContext(memberType);
                member  = context.Contract.Root;
            }

            var shouldWriteTypeName = TryResolveValueType(value, ref member, out Type valueType);

            if (member.DefaultValue != null)
            {
                var defaultValueHandling = member.DefaultValueHandling ?? Settings.DefaultValueHandling;

                if (defaultValueHandling == XmlDefaultValueHandling.Ignore && value.Equals(member.DefaultValue))
                {
                    return;
                }
            }

            if (context == null || context.Contract.ValueType != member.ValueType)
            {
                context = Settings.GetTypeContext(valueType);
            }

            switch (member.MappingType)
            {
            case XmlMappingType.Element:
                writer.WriteStartElement(member.Name);

                if (initialState)
                {
                    initialState = false;
                    WriteNamespaces(writer);
                }

                if (shouldWriteTypeName)
                {
                    WriteTypeName(writer, valueType);
                }

                WriteXml(writer, value, member, context);
                writer.WriteEndElement();
                break;

            case XmlMappingType.Attribute:
                writer.WriteStartAttribute(member.Name);
                WriteXml(writer, value, member, context);
                writer.WriteEndAttribute();
                break;

            case XmlMappingType.InnerText:
                WriteXml(writer, value, member, context);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }