Ejemplo n.º 1
0
        protected override void Serialize(object target, XmlElementContext context)
        {
            _listBuilder?.Begin(GetValue(target), true);

            var content        = _listBuilder?.Objects;
            var targetElements = XmlElementSerializer
                                 .GetElements(context.XElement, true, _rootTarget, content?.Length ?? 0)?.ToArray();

            if (targetElements == null)
            {
                throw new InvalidOperationException("Serialize No Data Returned");
            }

            if (content == null)
            {
                return;
            }

            for (var i = 0; i < content.Length; i++)
            {
                _serializer.Serialize(content[i], targetElements[i]);
            }

            _listBuilder?.End();
        }
        protected override void Serialize(object target, XmlElementContext context)
        {
            var obj = XmlElementSerializer.GetElement(context.XElement, true, _xmlElementTarget);

            if (!(obj is XElement))
            {
                throw new InvalidOperationException("Attributes not Supported");
            }
            _serializer?.Serialize(context.XElement.CreateWriter(), GetValue(target));
        }
        protected override void Deserialize(object target, XmlElementContext context)
        {
            var obj = XmlElementSerializer.GetElement(context.XElement, false, _xmlElementTarget);

            if (!(obj is XElement ele))
            {
                return;
            }
            SetValue(target, _serializer?.Deserialize(ele.CreateReader(ReaderOptions.OmitDuplicateNamespaces)));
        }
        public XmlMapper(string?membername, Type targetType, SimpleConverter <string>?converter, XmlElementTarget target)
            : base(membername, targetType)
        {
            if (converter == null && TargetMember != null)
            {
                converter = ConverterFactory.CreateConverter(TargetMember, MemberType);
            }

            _serializer = new XmlElementSerializer(target, converter);
        }
Ejemplo n.º 5
0
        protected override SerializationContext GetRealContext(XmlElementContext origial, SerializerMode mode)
        {
            if (!(XmlElementSerializer.GetElement(origial.XElement, mode == SerializerMode.Serialize, _target) is XElement ele))
            {
                _useSnapShot = false;
                return(origial.Original);
            }

            _useSnapShot = true;
            return(origial.Original.CreateSnapshot(ele.Value));
        }
Ejemplo n.º 6
0
        private IEnumerable <XElement>?GetElements(bool forWrite, [NotNull] XElement context, int count)
        {
            var realRoot = XmlElementSerializer.GetElement(context, forWrite, _rootTarget) as XElement;

            if (realRoot == null && forWrite)
            {
                throw new InvalidOperationException("Attributes not Supported");
            }

            return(realRoot == null
                ? Enumerable.Empty <XElement>()
                : XmlElementSerializer.GetElements(realRoot, forWrite, _target, count));
        }
Ejemplo n.º 7
0
        public XmlListMapper(string?membername, Type targetType, XmlElementTarget rootTarget, XmlElementTarget target, SimpleConverter <string>?converter)
            : base(membername, targetType)
        {
            _rootTarget = rootTarget;

            if (MemberType != null)
            {
                _listBuilder = new ListBuilder(MemberType);
            }

            if (converter == null && _listBuilder != null)
            {
                converter = ConverterFactory.CreateConverter(TargetMember, _listBuilder.ElemenType);
            }

            _serializer = new XmlElementSerializer(target, converter);
        }
Ejemplo n.º 8
0
        protected override void Deserialize(object target, XmlElementContext context)
        {
            _listBuilder?.Begin(null, false);

            var targetElements = XmlElementSerializer.GetElements(context.XElement, false, _rootTarget, -1)?.ToArray();

            if (targetElements == null)
            {
                return;
            }

            foreach (var targetElement in targetElements)
            {
                _listBuilder?.Add(_serializer.Deserialize(targetElement));
            }

            SetValue(target, _listBuilder?.End());
        }