示例#1
0
 public static string FromModel <T>(T model, XmlOptions options)
 {
     try
     {
         return(ExtensibleMarkupLanguage.ModelToXml <T>(model, options));
     }
     catch (Exception ex)
     {
         throw new FormatSerializeException(Constants.Format.SERIALIZE_ERROR_MESSAGE, ex, FormatRange.Xml, model);
     }
 }
        public Response <string> FromModel <T>(T model, XmlOptions options)
        {
            var response = new Response <string>();

            try
            {
                var json = ExtensibleMarkupLanguage.ModelToXml <T>(model, options);
                response = response.With(json);
            }
            catch (Exception ex)
            {
                ex.LogValue($"Error deserializing format to type {typeof(T).FullName}: {ex}");
            }

            return(response);
        }
        public Maybe <string, FormatSerializeException> FromModel <T>(T model, XmlOptions options)
        {
            var maybe = new Maybe <string, FormatSerializeException>();

            try
            {
                var xml = ExtensibleMarkupLanguage.ModelToXml <T>(model, options);
                maybe = maybe.With(xml);
            }
            catch (Exception ex)
            {
                ex.LogValue($"Error serializing format to type {typeof(T).FullName}: {ex}");
                maybe = maybe.With(new FormatSerializeException(Constants.Format.SERIALIZE_ERROR_MESSAGE, ex, FormatRange.Xml, model));
            }

            return(maybe);
        }
示例#4
0
        /// <summary>Converts an object to its serialized XML format.</summary>
        /// <typeparam name="T">The type of object we are operating on.</typeparam>
        /// <param name="value">The object we are operating on.</param>
        /// <returns>The XML string representation of the object.</returns>
        public static string ModelToXml <T>(T value, XmlOptions options)
        {
            var namespaces = options.RemoveDefaultXmlNamespaces ? new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty }) : null;
            var settings   = new XmlWriterSettings
            {
                Indent             = true,
                OmitXmlDeclaration = options.OmitXmlDeclaration,
                CheckCharacters    = false
            };

            using (var stream = new StringWriterWithEncoding(options.Encoding))
                using (var writer = XmlWriter.Create(stream, settings))
                {
                    var serializer = new XmlSerializer(value.GetType());
                    serializer.Serialize(writer, value, namespaces);
                    return(stream.ToString());
                }
        }