예제 #1
0
        /// <summary>
        /// Deserializes the specified <paramref name="value" /> into an object of <paramref name="valueType"/>.
        /// </summary>
        /// <param name="value">The stream from which to deserialize the object graph.</param>
        /// <param name="valueType">The type of the deserialized object.</param>
        /// <returns>An object of <paramref name="valueType"/>.</returns>
        /// <remarks>This method will deserialize, in the order specified, using one of either:<br />
        /// 1. the explicitly defined <see cref="XmlFormatterOptions.ReaderFormatter" /> delegate<br />
        /// 2. the implicit or explicit defined delegate in <see cref="XmlFormatterOptions.ReaderFormatters" /> dictionary<br />
        /// 3. if neither was specified, a default XML reader implementation will be used.</remarks>
        public override object Deserialize(Stream value, Type valueType)
        {
            var serializer = Options.Converter;

            if (serializer == null)
            {
                var formatter = Options.ParseReaderFormatter(valueType);
                serializer = DynamicXmlConverter.Create(null, formatter, options =>
                {
                    options.ReaderSettings = Options.ReaderSettings;
                    options.RootName       = Options.RootName;
                });
            }
            return(XmlConvert.DeserializeObject(value, valueType, serializer));
        }
예제 #2
0
        /// <summary>
        /// Serializes the specified <paramref name="source"/> to an object of <see cref="Stream"/>.
        /// </summary>
        /// <param name="source">The object to serialize to XML format.</param>
        /// <param name="sourceType">The type of the object to serialize.</param>
        /// <returns>A stream of the serialized <paramref name="source"/>.</returns>
        /// <remarks>This method will serialize, in the order specified, using one of either:<br/>
        /// 1. the explicitly defined <see cref="XmlFormatterOptions.WriterFormatter"/> delegate<br/>
        /// 2. the implicit or explicit defined delegate in <see cref="XmlFormatterOptions.WriterFormatters"/> dictionary<br/>
        /// 3. if neither was specified, a default XML writer implementation will be used on <see cref="XmlConverter"/>.
        /// </remarks>
        public override Stream Serialize(object source, Type sourceType)
        {
            Validator.ThrowIfNull(source, nameof(source));
            Validator.ThrowIfNull(sourceType, nameof(sourceType));
            var serializer = Options.Converter;

            if (serializer == null)
            {
                var formatter = Options.ParseWriterFormatter(sourceType);
                serializer = DynamicXmlConverter.Create(formatter, null, options =>
                {
                    options.WriterSettings = Options.WriterSettings;
                    options.RootName       = Options.RootName;
                });
            }
            return(XmlConvert.SerializeObject(source, serializer));
        }
예제 #3
0
 /// <summary>
 /// Inserts an XML converter to the list at the specified <paramref name="index" />.
 /// </summary>
 /// <typeparam name="T">The type of the object to converts to and from XML.</typeparam>
 /// <param name="converters">The list of XML converters.</param>
 /// <param name="index">The zero-based index at which an XML converter should be inserted.</param>
 /// <param name="writer">The delegate that converts <typeparamref name="T" /> to its XML representation.</param>
 /// <param name="reader">The delegate that generates <typeparamref name="T" /> from its XML representation.</param>
 /// <param name="canConvertPredicate">The delegate that determines if an object can be converted.</param>
 /// <param name="qe">The optional <seealso cref="XmlQualifiedEntity"/> that will provide the name of the root element.</param>
 public static void InsertXmlConverter <T>(this IList <XmlConverter> converters, int index, Action <XmlWriter, T, XmlQualifiedEntity> writer = null, Func <XmlReader, Type, T> reader = null, Func <Type, bool> canConvertPredicate = null, XmlQualifiedEntity qe = null)
 {
     converters.Insert(index, DynamicXmlConverter.Create(writer, reader, canConvertPredicate, qe));
 }