public void WriteXml(XmlWriter writer, object value, XmlSerializationContext context)
        {
            if (value == null)
            {
                return;
            }

            if (context.Member.MappingType != XmlMappingType.Element)
            {
                throw new XmlSerializationException($"XML mapping of \"{context.ValueType}\" must be Element.");
            }

            var target   = value;
            var contract = context.Contract.ToObjectContract();

            foreach (var property in contract.Properties)
            {
                if (CanWriteProperty(property))
                {
                    var propertyValue = GetValue(value, property);

                    if (!property.IsCollection)
                    {
                        context.Serialize(writer, propertyValue, property);
                    }
                    else
                    {
                        context.SerializeBody(writer, propertyValue, property);
                    }
                }
            }
        }
Example #2
0
 /// <summary>
 /// Serializes to string.
 /// </summary>
 /// <param name="obj">The obj.</param>
 /// <returns>System.String.</returns>
 /// <inheritdoc />
 public string SerializeToString(object obj)
 {
     CanSerialize(obj.GetType(), true);
     using (var sw = new StringWriter())
     {
         var writer = XmlWriter.Create(sw, Settings.GetWriterSettings());
         using (writer)
         {
             var context = new XmlSerializationContext(Settings);
             context.Serialize(writer, obj, obj.GetType());
         }
         return(sw.ToString());
     }
 }
Example #3
0
        /// <summary>
        /// Serializes to XML writer.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="writer">The writer.</param>
        /// <param name="value">The value.</param>
        /// <inheritdoc />
        public void SerializeToXmlWriter <T>(XmlWriter writer, T value) where T : class
        {
            writer.IsNullThrow(nameof(writer));

            value.IsNullThrow(nameof(value));
            var canSerialize = CanSerialize(typeof(T), true);

            using (writer)
            {
                var context = new XmlSerializationContext(Settings);
                context.Serialize(writer, value, value.GetType());
            }
            writer.Flush();
        }
Example #4
0
        /// <summary>
        /// Serializes to XML writer.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="valueType">Type of the value.</param>
        /// <param name="value">The value.</param>
        /// <inheritdoc />
        public void SerializeToXmlWriter(XmlWriter writer, Type valueType, object value)
        {
            writer.IsNullThrow(nameof(writer));
            valueType.IsNullThrow(nameof(valueType));
            value.IsNullThrow(nameof(value));
            var canSerialize = CanSerialize(valueType, true);

            using (writer)
            {
                var context = new XmlSerializationContext(Settings);
                context.Serialize(writer, value, valueType);
            }
            writer.Flush();
        }
Example #5
0
        /// <summary>
        /// Serializes to string.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj">The obj.</param>
        /// <returns>System.String.</returns>
        public string SerializeToString <T>(T obj) where T : class
        {
            var canSerialize = CanSerialize(typeof(T), true);

            using (var sw = new StringWriter())
            {
                var writer = XmlWriter.Create(sw, Settings.GetWriterSettings());
                using (writer)
                {
                    var context = new XmlSerializationContext(Settings);
                    context.Serialize(writer, obj, typeof(T));
                }
                return(sw.ToString());
            }
        }
Example #6
0
 protected override void SerializeMembers(XmlSerializationContext context)
 {
     context.Writer.WriteStartElement(prefix, local_name, @namespace);
     context.Serialize(Value);
     context.Writer.WriteEndElement();
 }